Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Query within Query - checking for multiple fields

Author  Topic 

MJ1210
Starting Member

3 Posts

Posted - 2008-06-09 : 17:07:55
Hi,
I remember seeing a fancy query that checked for multiple fields in a table (I think using a select statement in the where clause but not sure), but can't remember how to do it... here is what I want to do (and maybe there is a much easier way). Thanks!

Table1
id item color
1 shoe red
2 shoe blue
3 coat green
4 coat black

Table2
item color
shoe red
coat green

I want everything in Table1 where item and color are not a match.

So my results should be:
2 shoe blue
4 coat black

I'm sorry if this is a dumb question... it's been that kind of a day!

Thanks!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-09 : 17:11:20
SELECT t1.id, t1.item, t1.color
FROM Table1 t1
LEFT OUTER JOIN Table2 t2
ON t1.item = t2.item AND t1.color = t2.color
WHERE t2.item IS NULL and t2.color IS NULL

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -