I am not sure even how to ask this question.
I have a table of tags:
TagId Tag
----- -----
1 Fruit
2 Meat
3 Grain
I have a table of events:
EventId Event
------- -----------
1 Eating Food
2 Buying Food
What I need to do is bring back only Events that have all selected tags associated with it.
If three tags are selected then only show event that have all three.
For example:
Mapping Table
EventId TagId
------- -----
1 1
1 3
2 1
If I write a query like this:
select * from MapTable where where tagId in (1,3)
This will return Eating Food and Buying Food.
But what I need to do is bring back events that have both tags 1 and 3. This means that the only event in this case I would return would be Eating Food as it has both selected tags.
I was wondering if this can be done in TSQL or if I will have to use the business layer to translate it into the object to return back to the GUI.
Thanks.
There was a very similar question yesterday: Query for exact match of users in a conversation in SQL Server
basically you can do this:
so this will find all events that both the tags exist in (this allows for instances where those two tags exist along with any additional tags)