2013-03-12 57 views
0

假設我有兩個表如下,我想寫一個查詢和規則是 我想找出哪個orderID沒有attachmentID。 如何在SQL中查詢它?SQL Server查詢幫手

OrderID  Name 
1    Computer  
2    Laptop  
3    Tablet  

AttachmentID  OrderID  Url 
1      1  …. 
2      2  …. 
+0

所以你正在尋找'OrderID = 3'?這就是'LEFT JOIN'尋找'AttachmentID IS NULL'的工作。 – 2013-03-12 20:14:52

回答

1
select * 
from Orders o 
where not exists 
     (
     select * 
     from Attachments a 
     where a.OrderID = o.OrderID 
     ) 
1

另一種可能的答案,假設SQL服務器

select OrderID from Orders 
EXCEPT 
select OrderID from Attachments 

MSDN doc for Except and Intersect獲取更多信息。