检查其内的多个选择语句子查询“不”条件子查询与多个SELECT语句
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)
,而不是重复“不”条件相同的id,我需要将一次性检查多个表的子查询..
请帮助..
检查其内的多个选择语句子查询“不”条件子查询与多个SELECT语句
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)
,而不是重复“不”条件相同的id,我需要将一次性检查多个表的子查询..
请帮助..
你可以使用一个工会,所以你只要有一个in
:
select id
from tbl
where id not in
(
select id from table1
union all select id from table2
union all select id from table3
)
注意:not in
不适用于可为空的列,但我认为id
在此处不可空。
我会有兴趣比较查询计划与这个答案和我.... – 2011-05-21 14:16:31
您所查询的是更好表现为:
SELECT id
FROM tbl t
LEFT JOIN table1 t1 on t1.id = t.id
LEFT JOIN table2 t2 on t2.id = t.id
LEFT JOIN table3 t3 on t3.id = t.id
WHERE t1.id IS NULL AND t2.id IS NULL AND t3.id IS NULL
(重新编号你的表,因为你已经加入table1两次,而根本没有表3) – 2011-05-21 10:07:36
谢谢,我是一个可怕的打字员! – 2011-05-21 10:07:58
你的表面上紧急的问题已经在下面回答..... – 2011-05-21 10:26:36