I have a list of ids:
(1, 2, 3, 6, 7)
And a table:
id | anothercolumn
------------------
1 | NULL
2 | foo
4 | bar
5 | NULL
6 | NULL
I want to retrieve the values from my list which are not ids of my table.
Expected result:
3, 7
SELECT without FROM
I tried something like this:
SELECT i
WHERE i IN (1, 2, 3, 6, 7)
AND i NOT IN (SELECT id FROM mytable);
But this is not a valid MySQL query (a FROM
is required).
UNION
There is also this possibility:
SELECT i
FROM (
SELECT 1 AS i
UNION SELECT 2
UNION SELECT 3
UNION SELECT 6
UNION SELECT 7 ) AS mylistofids
LEFT JOIN mytable
ON mytable.id = i
WHERE mytable.id IS NULL;
This works, but if my list of ids becomes bigger, the query will soon be huge ...
Temporary Table
I can also create a temporary table for my list of ids:
CREATE TEMPORARY TABLE mylistofids (
i INT
);
INSERT INTO mylistofids (i) VALUES (1), (2), (3), (6), (7);
Then use it in a LEFT JOIN
:
SELECT i
FROM mylistofids
LEFT JOIN mytable
ON mytable.id = i
WHERE mytable.id IS NULL;
This works too, but in my case, I don't have the rights to create a table (temporary or not).
Do you see a way to solve this problem in the nicest way possible?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…