How to retrieve multiple tags from a database?
Alright, I have done some research and found out that a good normalized articles and tags table should be like this:
articles table: article_id | article_text
tags t开发者_运维百科able: tag_id | tag_text
article-tag table: article_id | tag_id
Now, what I try to do is this: let's say I am searching for 3 tags for example, "asp.net", ".net", "c#". It is very easy to retrieve articles which has these tags. But I don't want this. What I want is to retrieve articles which has exactly these three tags or any two or one of them but not articles with 4 or more tags including them. I don't want a search result with "asp.net", ".net", "c#", "ruby". I want only following tagged articles:
"asp.net", ".net", "c#"
.net", "c#"
"asp.net", ".net"
"asp.net", "c#"
"c#"
"asp.net"
".net"
What would be the right MySQL query for that?
I'm thinking that a self join might be the way. If you were querying by three tags, you would join the article-tag table with itself 3 times, for example. I'm not sure how this would scale, though.
Update: with this approach you would need to do a second query to exclude those matches with > 3 tags.
精彩评论