views:

47

answers:

2

I have an array which contains a bunch of ID:s...

I can't figure out how to write a query for finding all records which are NOT inside this array, in mysql.

    SELECT * FROM main_table WHERE ..........

Any ideas?

Thanks

+8  A: 

Like this:

$str = implode(',', $your_array);

The above statements converts an array into comma-delimited string.

"SELECT * FROM main_table WHERE id NOT IN ('$str')"

More Info:

Sarfraz
+1 for PHP integration
Nicolas78
Little tweak: `$str = count($your_array) != 0 ? implode(',', $your_array); : "null";`. `IN` doesn't react well to empty braces.
Femaref
+3  A: 
SELECT * 
  FROM main_table
 WHERE id NOT IN(1, 2, 3)
Aillyn