Deleting rows not in an array
Moderator: General Moderators
Deleting rows not in an array
I have an array with the numbers 22, 24, 26 for example.
I have a table with rows. I would like to delete rows not in the array.
How would I achieve this?
I have a table with rows. I would like to delete rows not in the array.
How would I achieve this?
Code: Select all
DELETE .... WHERE xyz NOT IN (22,24,26)- ReverendDexter
- Forum Contributor
- Posts: 193
- Joined: Tue May 29, 2007 1:26 pm
- Location: Chico, CA
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
That's... not quite correct either. I believe it will create associative entries that include brackets. If not and I'm somehow mistaken, it's still redundant.Smasher wrote:Okay the array is actually
array([0] => 22, [1] => 24, [2] => 26);
Code: Select all
$array = array(22, 24, 26);Using the following.
Worked fine
Thanks for your help once again

Code: Select all
$string = '';
foreach($segments as $segment) {
$string .= ", $segment";
}
$db->query("DELETE FROM ens.my_table WHERE `id` NOT IN (".substr($string, 2).") AND `some_id` = '$someId'");Worked fine
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Howso?Jcart wrote:no need for a loop, you can use implode() to create the string
Code: Select all
$segments = implode(",", $segments);
$db->query("DELETE FROM ens.my_table WHERE `id` NOT IN ($segments) AND `some_id` = '$someId'");