I've got a mental block. I'm pretty certain this is a dead simple noob question to solve, but I'm drawing a blank:
I have a tagging system for articles. This is done by having a separate table that contains an article ID as well as a tag ID, so multiple tags can get assigned to one article and vice-versa. It all works well. But now what I want to do is to filter articles based on whether they match two or more tags, or match two or more tags but not specific ones, or match all the tags specified, and so on.
--------------------
|ID|ArticleID|TagID|
--------------------
|1 |4000 |123 |
|2 |4000 |3532 |
|3 |4000 |4386 |
|4 |4001 |3532 |
etc...
--------------------
So it should return:
- 4000 and 4001 if I only search for 3532
- 4000 if I say I want only stuff that matches 123 and 4386
- 4000 and 4001 if I want it to match 123 or 3532
- 4001 if I want it to match 3532 but not 123.
My ideas so far have involved going "select articleid where tagid = 123 and tagid = 4386" but obviously it's impossible for the tagid field to be two things on the same record (using "or" would get me the results, but it wouldn't ensure that it was only things that matched both 123 and 4386). Next up I was going to query them one condition at a time and then use PHP to filter through which articles should/shouldn't match, but there's the nagging feeling at the back of my mind that this should be able to do be done easily at the database level, I just can't think of how (or what to Google for). I'm hoping to be able to filter upwards of 1000 tags at a time.