我有一个包含 ID 的表和一个 ID 列表 (1,3,4,5,2,29,24)。我可以通过此类查询查看该列表中的哪些 ID:
select * from tbl1 where id in (1,3,4,5,2,29,24)
但是是否可以在不创建临时表的情况下从这个 id 列表中看到不存在于 tbl1 中的数字?有点像
select * from tbl1 where (1,3,4,5,2,29,24) not in id
最佳答案
您可以在查询中使用派生表:
select id
from (select 1 as id union all select 3 union all select 4 union all select 5 union all
select 2 union all select 29 union all select 24
) id
where not exists (select 1 from tbl1 where tbl1.id = id.id);
如果您想查看表中和不在表中的 ID,请使用 left join
:
select id.id, tbl1.*
from (select 1 as id union all select 3 union all select 4 union all select 5 union all
select 2 union all select 29 union all select 24
) id left join
tbl1
on tbl1.id = id.id;
关于mysql - mysql中的条件反转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34093373/