0

我有两个具有共同 id 的表,table1 有一个任务编号列,table2 有文档列,每个任务可以有多个文档。我正在尝试查找所有没有特定文档的任务编号

假数据:

SELECT * FROM table1
id  tasknumber
1   3210-012
2   3210-022
3   3210-032

SELECT * FROM table2
id  document
1   revision1
1   SB
1   Ref
2   revision1
2   Ref
3   revision1
3   SB

但是我如何找到没有名为 SB 的文档的任务编号?

4

4 回答 4

3
SELECT t1.tasknumber
FROM   table1 t1
LEFT   JOIN table2 t2 ON t2.id = t1.id AND t2.document = 'SB'
WHERE  t2.id IS NULL;

基本上有四种技术:

于 2013-10-21T18:55:23.963 回答
1
select
    tasknumber
from 
    table1
where
    not exists (select 1
                  from table2
                 where table1.id = table2.id
                       and table2.document = 'SB');

或者

select
    tasknumber
from 
    table1
where
    id not in (select id
                 from table2 
                where document = 'SB');
于 2013-10-21T18:56:29.237 回答
1
select t1.tasknumber from table1 t1
where not exists 
     (select 1 from table2 t2 where t1.id = t2.id and t2.document = 'SB')
于 2013-10-21T18:55:35.947 回答
0

用这个:

SELECT TASKNUMBER 
FROM TABLE1
WHERE ID NOT IN (SELECT DISTINCT ID FROM TABLE 2 where document = 'SB') 
于 2013-10-21T18:55:47.337 回答