MySQL - Select from a list of numbers those without a counterpart in the id field of a table MySQL - Select from a list of numbers those without a counterpart in the id field of a table mysql mysql

MySQL - Select from a list of numbers those without a counterpart in the id field of a table


This is a problem that is pretty common: generating a relation on the fly without creating a table. SQL solutions for this problem are pretty awkward. One example using a derived table:

SELECT n.idFROM  (SELECT 2 AS id    UNION SELECT 3    UNION SELECT 4    UNION SELECT 5    UNION SELECT 6    UNION SELECT 7) AS n  LEFT OUTER JOIN foos USING (id)WHERE foos.id IS NULL;

But this doesn't scale very well, because you might have many values instead of just six. It can become tiresome to construct a long list with one UNION needed per value.

Another solution is to keep a general-purpose table of ten digits on hand, and use it repeatedly for multiple purposes.

CREATE TABLE num (i int);INSERT INTO num (i) VALUES (0), (1), (2), (3), (4), (5), (6), (7), (8), (9);SELECT n.idFROM   (SELECT n1.i + n10.i*10 AS id   FROM num AS n1 CROSS JOIN num AS n10   WHERE n1.i + n10.i*10 IN (2, 3, 4, 5, 6, 7)) AS n  LEFT OUTER JOIN foos USING (id)WHERE foos.id IS NULL;

I show the inner query generating values from 0..99 even though this isn't necessary for this case. But you might have values greater than 10 in your list. The point is that with one table num, you can generate large numbers without having to resort to very long chains with one UNION per value. Also, you can specify the list of desired values in one place, which is more convenient and readable.


I can't find a solution to your precise problem that doesn't use a temporary table, but an alternate way of doing your query using a sub-select instead of a join is:

SELECT bars.* FROM bars WHERE bars.ID NOT IN (SELECT ID FROM foos)

Like the other posters I originally wrote:

SELECT * FROM foos WHERE foos.ID NOT IN (2, 4, 5, 6, 7)

but then I realised that this is producing the opposite to what you want.


If you use PHP, you can make this work without creating any temporary tables.

SELECT ID FROM foos WHERE foos.ID IN (2, 4, 5, 6, 7)

You can use PHP's array_diff() function to convert this to the desired result. If your list (2,4,5,6,7) is in an array called $list and the result of the query above is in an array $result, then

$no_counterparts = array_diff($list, $result);

...will return all the numbers in your list with no counterpart in your database table. While this solution doesn't perform the entire operation within the query, the post-processing you need to do in PHP is minimal to get what you want, and it may be worthwhile to avoid having to create a temporary table.