首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mysql中的inverse condition

mysql中的inverse condition
EN

Stack Overflow用户
提问于 2015-12-05 00:50:01
回答 2查看 1.1K关注 0票数 1

我有一个包含ids和ids列表(1,3,4,5,2,29,24)的表。我可以使用下面的查询在表中查看该列表中的哪些in:

代码语言:javascript
复制
select * from tbl1 where id in (1,3,4,5,2,29,24)

但是,是否可以在不创建临时表的情况下查看此is列表中不存在于tbl1中的数字?就像这样

代码语言:javascript
复制
select * from tbl1 where (1,3,4,5,2,29,24) not in id
EN

回答 2

Stack Overflow用户

发布于 2015-12-05 00:52:50

您可以在查询中使用派生表:

代码语言:javascript
复制
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);

如果您想查看既在表中又不在表中的ids,则使用left join

代码语言:javascript
复制
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;
票数 0
EN

Stack Overflow用户

发布于 2015-12-05 08:46:23

select * from tbl1 where id not in (1,3,4,5,2,29,24)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34093373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档