简体   繁体   中英

Many-to-one relationship to implement tags: How to count how many times a tag is used?

I'm experimenting with a tagging system which is a many-to-one relationship. My schema is:

items table:

  • item_id
  • comment

comment _ tags :

  • item_id
  • tag_id

tags table:

  • tag_id
  • tag_name

I've been reading of implementation designs at the links at the bottom but got stuck. I can insert tags without a problem.

How do I fetch every tag that has been used and get how many times it has been applied to an item in the comment _ tags table?

http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html

SQL Query for Product-Tag relationship

SELECT t.tag_name, COUNT(*)
FROM tags AS t
    INNER JOIN comment_tags AS c_t ON c_t.tag_id = t.tag_id
GROUP BY c_t.tag_id
ORDER BY t.tag_name;

not the correct names, but mostly correct syntex

select TagName, count(TagName)
from TagTable
group by TagName

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM