2017-01-25 53 views
-3

我有2個表:(這些都不是真實的表,我做了簡單的演示)SQL語法 - 相反選擇

> 1. Students_Table - with ID column (and others) 
> 2. Students_Pictures_Table : with ID column and PIC(Blob) column 

我需要SQL語法,只返回學生ID爲那些 存在於Students_Table中但不存在於Students_Pictures_Table中。

+2

MySQL和DB2?請不要標記不涉及的產品。你有什麼嘗試?爲什麼它不工作? http://stackoverflow.com/help/how-to-ask – HoneyBadger

+0

添加一些示例表格數據和預期結果 - 以及格式化文本! – jarlh

+1

提示:「不存在」。 – jarlh

回答

2

not exists和相關子查詢可能是最簡單的方法

select s.id 
from students s 
where not exists (select 1 from student_pictures p where p.id = s.id) 
1

您可以使用left join讓行,例如:

SELECT s.id 
FROM Students s LEFT JOIN Students_Pictures sp ON s.id = sp.student_id 
WHERE sp.id IS NULL 
GROUP BY s.id; 
+0

謝謝!有用。祝你有美好的一天! – user2204888