Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with SQL

Status
Not open for further replies.

mark1110

Programmer
Apr 20, 2005
85
US
Hi,

I have two tables (table1 and table2). They both contain two fields (ID and state). What I would like is to list all the ID's in table1 that are either not in table2 or if the ID is in table2 to see if the state in table1 is not in table2 with matching ID's.

For example in table1 you have an ID of 1234 and a state of IL. In table2 if 1234 is not there to list it or if 1234 is in table2 and table2 has 1234 IN, 1234 OK, 1234 OR to list the ID because it does not have 1234 IL.

I have tried several different ways with not luck. Could someone please help me out.

Thanks,

Mark
 
SELECT T1.ID, T1.state
FROM table1 T1 LEFT JOIN table2 T2
ON T1.ID = T2.ID AND T1.state = T2.state
WHERE T2.ID IS NULL

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
SELECT T1.ID, T1.state, [blue]T2.state[/blue]
FROM table1 T1 LEFT JOIN table2 T2
ON T1.ID = T2.ID
WHERE [blue]COALESCE(T2.state,'') <> T1.state[/blue]

:)

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top