简体   繁体   English

从表中选择与预定义不匹配的值

[英]Select values from table which not match predefined

I have several values, generated from PHP. 我有几个从PHP生成的值。 Like 1, 10, 15, 17, 20. Also I have a table with several values, like: 像1、10、15、17、20。此外,我还有一个包含多个值的表,例如:

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. 我想编写一个查询,该查询将为我提供所有编号,这些编号在表id列中不存在。 In my example, values of 17 and 20 should be returned. 在我的示例中,应返回值1720

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. 然后使用一些PHP数组函数来获取数组的减法。

You can use IN for that: 您可以为此使用IN:

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

MySQL documentation MySQL文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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