tags:

views:

41

answers:

1

Hi, I have a simple question here (I know it is so simple but I just got stuck...)

I have a table
tag_id, search_id
1,1
2,1
4,2
2,2
5,2

I would like to get the search_id by giving the tag_ids example: tag_id 1,2 would return search_id 1 tag_id 4,2,5 would return search_id 2 tag_id 2,6 would return no result.

I should have pay more attention when I was in college... thanks in advance!

+2  A: 
SELECT search_id 
FROM YourTable
WHERE tag_id IN (4,2,5)
GROUP BY search_id
HAVING COUNT(DISTINCT tag_id) = 3 /*Adjust this as required*/
Martin Smith
Out of curiosity, is the IN clause an exact equivalent to WHERE (tag_ID = 4 OR tag_ID = 2 OR tag_ID = 5) ?
Tom Gullen
@Tom - Semantically yes. I'm not sure if MySQL's query optimiser treats it any differently though.
Martin Smith
+1 very very nice.
Conrad Frix
Thanks, always wondered.
Tom Gullen
Cool! thanks, looks nice and seems working good here!Owe you a beer there!!! :)
utp