我必须找到我为其提供ID的所有记录,所以这是我的查询

SELECT * FROM `inf_company` where id IN(25523,25511,25448,28094,25243.....)


我在上述查询中提供了500个ID,但是却提供了200条记录。我想找到剩下的300条记录。

我想在列表中查看所有未显示的ID。如何查询呢?

最佳答案

您可以为union字段写一个id,然后为left outer join编写以获得实际结果:

select id
,      case
       when i1.id is not null
       then 'EXISTING'
       else 'NON-EXISTING'
       end
       status
from   ( select 25523 id union select 25511 union select 25448 /* etc, etc */ ) x
left
outer
join   inf_company i1
on     i1.id = x.id


您也可以按照Strawberry的建议将id放在临时表中。这样可以防止union上的id列表过长。

关于mysql - 从in子句中获取缺失值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25500163/

10-12 22:42
查看更多