count - MySQL Tag System (Toxi) -


I have a tagging system with 3 tables:

  post-field: id, TITLE TAGMAP - Field: POSTID, TAGID Tag - Field: ID, NAME  

Currently, I use this query to get the number of posts for each tag:

  SELECT t.id, t.name, (SELECT COUNT (*) Tag with AS tagmap where the tagmap tag = t.id) ASC  

Gives me a list of tags, with post number for each :

  ID name number ------------------- --- 1 banana 8 2 apple 4 3 pudding 7 4 lemon 3  

Now, I have a search engine that allows you to click a tag, and get related posts that tag if I click on "apple" -Gold posts are available. But some of these positions also have "banana" and "pudding" tags. To create a new menu with updated post count, I need to know the count of each.

So if I click on apple, then there will be 4 posts. In 2 of these 4 posts there is a "pudding" tag, and 3 has the "lemon" tag. I would like an output like this:

  ID name number ---------------------- 1 banana 1 2 apple 4 3 If we continue this way, then if I click on "lemon" (hence we are loading posts related to "apple" and "lemon"), and 1 of 1 is the post "lemon "Tag is also the" pudding "tag, I would need an output like that:  
  ID name COUNT ------------ ------ ---- 1 banana 0 2 apple 4 3 pudding 1 4 lemon 3  

I'm not sure Land that is clear - if Please let me know if you need more information.

  select matchTags.id, matchTags.name, COUNT (matchTagMap.tagid) from AS calculation FROM (SearchTag from the tag as search tag Find the tagmap as search tag SearchTags.id = searchTagMap.tagid WHERE searchTags.name IN ('apple', 'pudding') SearchTagMap.postid hogging group by group (*) = 2) Touch Tag as Tag Tag Tag as DT Cross Join Tag Mail Match. Mettag dt.postid = matchTagMap.postid on Mac and matchTagMap.tagid = matchTags.id by group matchTags.id;  

Adjust the list of tag names and the HAVING COUNT (*) row to match.


Comments