SQL子查询:筛选不返回任何行的条目

时间:2013-03-04 12:23:01

标签: sql join subquery

我正在努力进行SQL查询...我有三个表:

1)工作

2)paper1

3)paper2

paper1paper2有一个列job_id,其中引用了相应的job-id;因此,ID 123 的作业的paper1.job_id 123

现在我想看到所有具有相应paper1但没有paper2的作业(因此:未完成,因为 paper1 paper2 是要完成的工作要求)。我的尝试是这样的,但它没有正确解决:

SELECT id 
FROM   jobs 
WHERE  (SELECT id FROM paper1 WHERE (job_id = jobs.id) != 0 LIMIT 1) 
AND    (SELECT id FROM paper2 WHERE (job_id = jobs.id)  = NULL LIMIT 1)

感谢任何帮助;感谢。

3 个答案:

答案 0 :(得分:1)

select id 
from jobs 
where exists (select p1.id from paper1 as p1 where p1.job_id = jobs.id)
    and not exists (select p2.id from paper2 as p2 where p2.job_id = jobs.id)

答案 1 :(得分:0)

根据我的理解,我认为你只想要只用纸1但不用纸2的工作 如果那是要求,那么尝试下面的代码

select * from jobs where id in (select distinct j1 from 
(select a.job_id as j1,b.job_id as j2 from paper1 a,paper2 b where a.job_id=b.job_id(+)) 
where j2 is null)

答案 2 :(得分:0)

尝试:

Select job_id 
FROM paper1 
WHERE job_id NOT IN (Select job_id from paper2 group by paper2)

GROUP BY job_id;