Can't figure out how to list all the people that don't live in same City as...

Posted by AspOnMyNet on Stack Overflow See other posts from Stack Overflow or by AspOnMyNet
Published on 2010-04-23T20:04:36Z Indexed on 2010/04/23 20:13 UTC
Read the original article Hit count: 346

Filed under:

I’d like to list all the people ( Person table ) that don’t live in same city as those cities listed in Location table. Thus, if Location table holds a record with City=’New York’ and State=’Moon’, but Person table holds a record with FirstName=’Someone’, City=’New York’ and Location=’Mars’, then Someone is listed in the resulting set, since she lives in New York located on Mars and not New York located on Moon, thus we’re talking about different cities with the same name. I tried solving it with the following query, but results are wrong:

SELECT Person.FirstName, Person.LastName,
Person.City, Person.State
FROM Person INNER JOIN Location
ON (Person.City <> Location.City AND Person.State = Location.State)
OR (Person.City = Location.City AND Person.State <> Location.State)
OR (Person.City <> Location.City AND Person.State <> Location.State)
ORDER BY Person.LastName;

Any ideas?

© Stack Overflow or respective owner

Related posts about sql