Delete data present in db and not present in current for loop
up vote
0
down vote
favorite
foreach($data as $key5 => $value5){
if($key5 === 'ID') {
$id=$value5;
}
$sql = "INSERT INTO offers
(id,status)
VALUES ('$id','$status')
ON DUPLICATE KEY
UPDATE status = 'updated'";
}
I'm inserting some ids to the database. If the id already exists in the table the status will be updated else it will be created.
I need to delete the ids which are already present in the table and not present in the current FOR loop.
Which is the best way to delete the ids?
Thanks in advance
php mysql sql-delete
add a comment |
up vote
0
down vote
favorite
foreach($data as $key5 => $value5){
if($key5 === 'ID') {
$id=$value5;
}
$sql = "INSERT INTO offers
(id,status)
VALUES ('$id','$status')
ON DUPLICATE KEY
UPDATE status = 'updated'";
}
I'm inserting some ids to the database. If the id already exists in the table the status will be updated else it will be created.
I need to delete the ids which are already present in the table and not present in the current FOR loop.
Which is the best way to delete the ids?
Thanks in advance
php mysql sql-delete
This will likely be easier to handle at the application level. Get all of the IDs in the database, then remove from that new set any IDs in your$data
variable. From there, delete that set from the database. Logically this isn't an ideal design imo, as you scale to larger amounts of records this will get slower.
– Rogue
Nov 21 at 14:37
1
In that case would it not be easier to just empty the table and then add all the new ones that are in this foreach loop
– RiggsFolly
Nov 21 at 14:38
Why is$id
only set when$key5 === 'ID'
?
– Nigel Ren
Nov 21 at 14:39
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
foreach($data as $key5 => $value5){
if($key5 === 'ID') {
$id=$value5;
}
$sql = "INSERT INTO offers
(id,status)
VALUES ('$id','$status')
ON DUPLICATE KEY
UPDATE status = 'updated'";
}
I'm inserting some ids to the database. If the id already exists in the table the status will be updated else it will be created.
I need to delete the ids which are already present in the table and not present in the current FOR loop.
Which is the best way to delete the ids?
Thanks in advance
php mysql sql-delete
foreach($data as $key5 => $value5){
if($key5 === 'ID') {
$id=$value5;
}
$sql = "INSERT INTO offers
(id,status)
VALUES ('$id','$status')
ON DUPLICATE KEY
UPDATE status = 'updated'";
}
I'm inserting some ids to the database. If the id already exists in the table the status will be updated else it will be created.
I need to delete the ids which are already present in the table and not present in the current FOR loop.
Which is the best way to delete the ids?
Thanks in advance
php mysql sql-delete
php mysql sql-delete
edited Nov 21 at 14:37
David.J
517521
517521
asked Nov 21 at 14:32
user8429002
235
235
This will likely be easier to handle at the application level. Get all of the IDs in the database, then remove from that new set any IDs in your$data
variable. From there, delete that set from the database. Logically this isn't an ideal design imo, as you scale to larger amounts of records this will get slower.
– Rogue
Nov 21 at 14:37
1
In that case would it not be easier to just empty the table and then add all the new ones that are in this foreach loop
– RiggsFolly
Nov 21 at 14:38
Why is$id
only set when$key5 === 'ID'
?
– Nigel Ren
Nov 21 at 14:39
add a comment |
This will likely be easier to handle at the application level. Get all of the IDs in the database, then remove from that new set any IDs in your$data
variable. From there, delete that set from the database. Logically this isn't an ideal design imo, as you scale to larger amounts of records this will get slower.
– Rogue
Nov 21 at 14:37
1
In that case would it not be easier to just empty the table and then add all the new ones that are in this foreach loop
– RiggsFolly
Nov 21 at 14:38
Why is$id
only set when$key5 === 'ID'
?
– Nigel Ren
Nov 21 at 14:39
This will likely be easier to handle at the application level. Get all of the IDs in the database, then remove from that new set any IDs in your
$data
variable. From there, delete that set from the database. Logically this isn't an ideal design imo, as you scale to larger amounts of records this will get slower.– Rogue
Nov 21 at 14:37
This will likely be easier to handle at the application level. Get all of the IDs in the database, then remove from that new set any IDs in your
$data
variable. From there, delete that set from the database. Logically this isn't an ideal design imo, as you scale to larger amounts of records this will get slower.– Rogue
Nov 21 at 14:37
1
1
In that case would it not be easier to just empty the table and then add all the new ones that are in this foreach loop
– RiggsFolly
Nov 21 at 14:38
In that case would it not be easier to just empty the table and then add all the new ones that are in this foreach loop
– RiggsFolly
Nov 21 at 14:38
Why is
$id
only set when $key5 === 'ID'
?– Nigel Ren
Nov 21 at 14:39
Why is
$id
only set when $key5 === 'ID'
?– Nigel Ren
Nov 21 at 14:39
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
You can save all the IDs in an array and at the end of your foreach
you can use a query to delete all elements that are NOT IN
your array:
$ids = array();
foreach($data as $key5 => $value5){
if($key5 === 'ID') {
$id=$value5;
$ids = $id;
}
$sql = "INSERT INTO offers
(id,status)
VALUES ('$id','$status')
ON DUPLICATE KEY
UPDATE status = 'updated'";
}
$delete_sql = "DELETE FROM offers WHERE id NOT IN ('".implode("','", $ids)."')";
add a comment |
up vote
0
down vote
There are many ways in which this could be done. Try this one...
$ids = array(); # to store existing ids
foreach($data as $key5 => $value5){
if($key5 === 'ID') {
$id = $value5;
$ids = $id; # collect all existing ids
}
$sql = "INSERT INTO offers
(id,status)
VALUES ('$id','$status')
ON DUPLICATE KEY
UPDATE status = 'updated'";
}
$deleteSql = "DELETE FROM `offers`
WHERE `id` NOT IN (" . implode(',' $ids) . ')';
// then run your $deleteSql query ...
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You can save all the IDs in an array and at the end of your foreach
you can use a query to delete all elements that are NOT IN
your array:
$ids = array();
foreach($data as $key5 => $value5){
if($key5 === 'ID') {
$id=$value5;
$ids = $id;
}
$sql = "INSERT INTO offers
(id,status)
VALUES ('$id','$status')
ON DUPLICATE KEY
UPDATE status = 'updated'";
}
$delete_sql = "DELETE FROM offers WHERE id NOT IN ('".implode("','", $ids)."')";
add a comment |
up vote
0
down vote
You can save all the IDs in an array and at the end of your foreach
you can use a query to delete all elements that are NOT IN
your array:
$ids = array();
foreach($data as $key5 => $value5){
if($key5 === 'ID') {
$id=$value5;
$ids = $id;
}
$sql = "INSERT INTO offers
(id,status)
VALUES ('$id','$status')
ON DUPLICATE KEY
UPDATE status = 'updated'";
}
$delete_sql = "DELETE FROM offers WHERE id NOT IN ('".implode("','", $ids)."')";
add a comment |
up vote
0
down vote
up vote
0
down vote
You can save all the IDs in an array and at the end of your foreach
you can use a query to delete all elements that are NOT IN
your array:
$ids = array();
foreach($data as $key5 => $value5){
if($key5 === 'ID') {
$id=$value5;
$ids = $id;
}
$sql = "INSERT INTO offers
(id,status)
VALUES ('$id','$status')
ON DUPLICATE KEY
UPDATE status = 'updated'";
}
$delete_sql = "DELETE FROM offers WHERE id NOT IN ('".implode("','", $ids)."')";
You can save all the IDs in an array and at the end of your foreach
you can use a query to delete all elements that are NOT IN
your array:
$ids = array();
foreach($data as $key5 => $value5){
if($key5 === 'ID') {
$id=$value5;
$ids = $id;
}
$sql = "INSERT INTO offers
(id,status)
VALUES ('$id','$status')
ON DUPLICATE KEY
UPDATE status = 'updated'";
}
$delete_sql = "DELETE FROM offers WHERE id NOT IN ('".implode("','", $ids)."')";
answered Nov 21 at 14:42
Mojo Allmighty
645316
645316
add a comment |
add a comment |
up vote
0
down vote
There are many ways in which this could be done. Try this one...
$ids = array(); # to store existing ids
foreach($data as $key5 => $value5){
if($key5 === 'ID') {
$id = $value5;
$ids = $id; # collect all existing ids
}
$sql = "INSERT INTO offers
(id,status)
VALUES ('$id','$status')
ON DUPLICATE KEY
UPDATE status = 'updated'";
}
$deleteSql = "DELETE FROM `offers`
WHERE `id` NOT IN (" . implode(',' $ids) . ')';
// then run your $deleteSql query ...
add a comment |
up vote
0
down vote
There are many ways in which this could be done. Try this one...
$ids = array(); # to store existing ids
foreach($data as $key5 => $value5){
if($key5 === 'ID') {
$id = $value5;
$ids = $id; # collect all existing ids
}
$sql = "INSERT INTO offers
(id,status)
VALUES ('$id','$status')
ON DUPLICATE KEY
UPDATE status = 'updated'";
}
$deleteSql = "DELETE FROM `offers`
WHERE `id` NOT IN (" . implode(',' $ids) . ')';
// then run your $deleteSql query ...
add a comment |
up vote
0
down vote
up vote
0
down vote
There are many ways in which this could be done. Try this one...
$ids = array(); # to store existing ids
foreach($data as $key5 => $value5){
if($key5 === 'ID') {
$id = $value5;
$ids = $id; # collect all existing ids
}
$sql = "INSERT INTO offers
(id,status)
VALUES ('$id','$status')
ON DUPLICATE KEY
UPDATE status = 'updated'";
}
$deleteSql = "DELETE FROM `offers`
WHERE `id` NOT IN (" . implode(',' $ids) . ')';
// then run your $deleteSql query ...
There are many ways in which this could be done. Try this one...
$ids = array(); # to store existing ids
foreach($data as $key5 => $value5){
if($key5 === 'ID') {
$id = $value5;
$ids = $id; # collect all existing ids
}
$sql = "INSERT INTO offers
(id,status)
VALUES ('$id','$status')
ON DUPLICATE KEY
UPDATE status = 'updated'";
}
$deleteSql = "DELETE FROM `offers`
WHERE `id` NOT IN (" . implode(',' $ids) . ')';
// then run your $deleteSql query ...
answered Nov 21 at 14:42
marvinIsSacul
35516
35516
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414356%2fdelete-data-present-in-db-and-not-present-in-current-for-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
This will likely be easier to handle at the application level. Get all of the IDs in the database, then remove from that new set any IDs in your
$data
variable. From there, delete that set from the database. Logically this isn't an ideal design imo, as you scale to larger amounts of records this will get slower.– Rogue
Nov 21 at 14:37
1
In that case would it not be easier to just empty the table and then add all the new ones that are in this foreach loop
– RiggsFolly
Nov 21 at 14:38
Why is
$id
only set when$key5 === 'ID'
?– Nigel Ren
Nov 21 at 14:39