具有多个 select 语句的子查询
检查子查询在“不在”条件内是否有多个 select 语句,
例如。
select id from tbl where
id not in (select id from table1) and
id not in (select id from table2) and
id not in (select id from table3)
我需要子查询,而不是重复相同的 id“不符合”条件,它将从多个表中一次性签入..
请帮助..
to check the subquery having multiple select statement inside 'not in' condition
Eg.
select id from tbl where
id not in (select id from table1) and
id not in (select id from table2) and
id not in (select id from table3)
instead of repeating the same id 'not in' condition , i need the subquery which will check in one shot from multiple tables..
pls help..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的查询最好表达为:
Your query is better expressed as:
您可以使用联合,这样您就只有一个
in
:注意:
not in
不适用于可空列,但我假设id
此处不可为空。You could use a union, so you just have one
in
:Note:
not in
does not work well with nullable columns, but I assumeid
is not nullable here.使用全部联合
像这样-->
select f.FIRST_NAME from farmer f where f.ID in (select v.ID from Village v where v.ID in (1,2) union all select s.ID from state s where s.ID in (3,4) )
use union all
like this -->
select f.FIRST_NAME from farmer f where f.ID in (select v.ID from Village v where v.ID in (1,2) union all select s.ID from state s where s.ID in (3,4) )