Excludes one to many SQL join-based records

How can I write an SQL query that excludes a record if one (external) record from one to several connections meets a certain condition?

For example:

Details ID 1 2 Items DetailID Item 1 A 1 B 2 B 2 C 

How can I select the detailed records in which the elements do not contain "A"?

+6
sql sql-server
source share
3 answers
 SELECT * FROM details d WHERE NOT EXISTS ( SELECT * FROM items i WHERE i.DetailID == d.ID AND i.Item = 'A') 
+5
source share

building a systempuntoout solution:

 SELECT details.* FROM details LEFT OUTER JOIN items ON details.ID=items.DetailID AND items.Item = 'A' WHERE items.DetailID IS NULL 
+2
source share

Why not just use an INNER JOIN, for example:

 SELECT details.* FROM details INNER JOIN items ON details.ID=items.DetailID AND items.Item<> 'A' 
0
source share

All Articles