简体   繁体   中英

Select values from table which not match predefined

I have several values, generated from PHP. Like 1, 10, 15, 17, 20. Also I have a table with several values, like:

id
1
3
10
12
15

I want to write a query, which will give me all numbers, which do not exist in table id column. In my example, values of 17 and 20 should be returned.

it will be much easy if you store those numbers in a temporary table so you can do join, eg

SELECT  a.*
FROM    temporaryTable a
        LEFT JOIN tableWithNumbers b
            ON a.ID = b.ID
WHERE   b.ID IS NULL

I would

SELECT id FROM table WHERE id IN (1, 10, 15, 17, 20)

Which will get you those which are in your set.

And then use some PHP array function to get the substract of the arrays.

You can use IN for that:

SELECT id FROM table WHERE id NOT IN (1, 10, 15, 17, 20)

MySQL documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM