Select from table1 WHERE table2 contains ALL search parameters?

Assuming tags(note_id, tag) is declared UNIQUE or PK, then you can use: SELECT note_id, COUNT(tag) FROM tags WHERE tag IN ('fruit', 'vegetable', 'meat') GROUP BY note_id HAVING COUNT(tag) >= 3 Further answer based on OP's comment below. To get all tags for the records that match: SELECT * FROM tags INNER JOIN ( SELECT note_id, COUNT(tag) FROM tags WHERE tag IN ('fruit', 'vegetable', 'meat') GROUP BY note_id HAVING COUNT(tag) >= 3 ) search_results ON search_results. Note_id = tags.

Note_id.

But I still need to be able to return all tags associated with the note. This query would return 1 record per note, when there should be as many records per note as there are tags. – user214828 Nov 19 '09 at 18:51 Just join the solution above back to the tags table to get one record per tag.

– Larry Lustig Nov 19 '09 at 18:53 I added the SQL to do that JOIN into the original answer. – Larry Lustig Nov 19 '09 at 18:57.

Without a subselect, as per request: SELECT notes. * FROM notes JOIN tags ON tag. Note = notes.Id AND tag.

Name IN ('fruit','meat','vegetable') GROUP BY notes. Id HAVING COUNT(*) = 3 More efficient method would be: SELECT notes. * FROM ( SELECT to.

Note FROM tags to WHERE to. Name = 'meat' AND EXISTS ( SELECT NULL FROM tags ti WHERE ti. Note = to.

Note AND to. Name IN ('fruit', 'vegetable') LIMIT 1, 1 ) ) t JOIN notes ON note.Id = t. Note The trick here is to put the search on the most selective tag ('meat' in my example) on the first place.

This is close. I would like to also bring back the tags associated with the note (all tags, not just the ones searched on). SELECT notes.

*, tags. * ... This really should yield multiple records per note, depending on how many tags there are. – user214828 Nov 19 '09 at 18:43.

For this purpose, notes and tags (with a foreign key to notes) is sufficient, but for the sake of argument, having a separate xref table would still present the same problem. Select all notes (and respective tags) that have match all tags queried by. – user214828 Nov 19 '09 at 18:55.

If you prefer using the join ... on syntax (called explicit join), then check Bohemian's answer.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions