tags:

views:

121

answers:

1

I have a cross reference table and need to find common values of one ID where the second ID has multiple values given in the query. This is easy to do with an "OR" but I can't for the life of me figure out how to do it with an "AND". I'm sure there is an obvious solution that has been staring at me for these many hours but I can't find it despite much testing and searching. Any help is much appreciated.

cross_reference_table with columns: id, location, attendee

Using OR is easy:

SELECT DISTINCT location FROM cross_reference_table
WHERE attendee = 6 OR attendee = 7 OR attendee = 8;

But I need to return only those locations where attendees 6,7 and 8 were all in attendance. Again, I can't simply use an AND in the query structure above. I've been beating my head against this and simply can't make any progress. Thanks in advance for any suggestions.

+1  A: 

Try something like this

select distinct location
From cross_reference_table
where attendee =6 or attendee = 7 or attendee=8
group by attendee, location
having count(*)=3
Sparky
Sparky,Works like a charm. I had to remove attendee from GROUP BY but it works perfectly. Thank you very much. I wish I'd posted this question 24 hours ago. lol.Grey
Grey
Don't forget to mark this as the correct answer, otherwise Sparky doesn't get credit! :-)
Scott Smith