I have a set of values(keys), 1,2,3,4,5 and following table:
id key
------
1 2
2 8
3 4
If I need to check which of the given keys are in
Create temporary table with your keys:
CREATE TEMPORARY TABLE mykeys (`key` INT);
INSERT INTO mykeys VALUES (1),(2),(3),(4),(5);
Then use NOT IN:
SELECT `key`
FROM mykeys
WHERE `key` NOT IN (SELECT `key` FROM mytable)
Finally, drop your TEMP table if you must:
DROP TABLE mykeys
EDIT: Added SQLFiddle.
If you are using PostgreSQL which supports EXCEPT operator, and also VALUES
statement can be used to create row set from list of values, there is another, easier way to do this without temporary tables (SQLFiddle):
VALUES (1),(2),(3),(4),(5)
EXCEPT
SELECT key FROM mytable