2013-03-04 118 views
0

我掙扎在SQL查詢過濾條目...我有三個表:SQL子查詢:不返回行

1)工作

2)paper1

3)paper2

paper1paper2有一個列表job_id其中引用了相應的作業ID;所以具有編號的工作具有paper1.job_id的。

現在,我希望看到有相應的paper1但沒有paper2所有作業(這樣的:未完成的,因爲paper1paper2是這個職位的要求得到滿足)。我的嘗試是這樣的,但它沒有正確工作:

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) 

任何幫助表示讚賞;謝謝。

回答

0

嘗試作業:

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

GROUP BY job_id; 
+0

非常感謝!這種方法比阿納託利斯的方法快近12(!)倍 – Martin 2013-03-07 08:42:51

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) 
+0

輝煌;非常感謝。 – Martin 2013-03-04 12:48:27

0

根據我的理解,我認爲你只想要其中僅在paper1但不是在paper2 如果多數民衆贊成的要求,然後嘗試下面的代碼

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)