tags:

views:

73

answers:

4

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?

+1  A: 

Try this:

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

This query finds all people that live in a city that has a matching City in the Location table but where the state is different. If you want to find People who have no matching Location there see Ayman's answer.

vfilby
+1  A: 

Do you mean something like

select person.fn, person.ln, person.city, person.st from person, location where person.city not in (select location.city from location where location.st != person.st)

brismith
+2  A: 

Use a left join:

SELECT Person.FirstName, Person.LastName, Person.City, Person.State
FROM Person LEFT JOIN Location
ON (Person.City = Location.City AND Person.State = Location.State)
WHERE Location.City IS NULL
ORDER BY Person.LastName

Left joins include all rows from the "left" table (Person) even if there are no matches in the "right" table (Location). When there are no matches, the columns of the right table are set to NULL.

In this query, the WHERE condition limits the returned rows to those which don't have matches.

Ayman Hourieh
The question isn't entirely clear, this only works if asker is looking for people that do not have a matching location record. If that's we they wants, this is it but I am not sure that's what they are asking.
vfilby
I want to find all the people who don't live in any of the cities listed in Location table. If a person is living in the city with the same name as as one of the cities listed in the Location table, but if the two cities are in different states, then that person should be included in the final results set
AspOnMyNet
Uhm, I haven't yet learned about left joins, so I guess it won't be before tomorrow that I'll know will if this is what I was asking for
AspOnMyNet
@AspOnMyNet - In this case, my query should do what you want.
Ayman Hourieh
Alrighty, thank you all for helping
AspOnMyNet
+1  A: 

you should do a left outer join on the two tables, and then select the ones where the location is null

something like

select * from Person left outer join Location on p.city=l.city and p.state=l.state where l.id is null

Andrey