MySql PHP select count of distinct values from comma separated data (tags)

后端 未结 5 1647
感情败类
感情败类 2020-12-01 18:38

How can I select the count of distinct values from data that is stored as comma separated values in MySql? I\'ll be using PHP to output the data from MySql in the end.

5条回答
  •  日久生厌
    2020-12-01 19:42

    The recommended way of doing this is to not store multiple values in a single column, but create an intersection table.

    So, your tables would have these columns:
    1. tags: tag_id, name
    2. posts: post_id, category_code
    3. int_tags_to_posts: post_id, tag_id

    To get the counts:
    select t.name, count(*) from tags t, posts p, int_tags_to_posts i where i.post_id = p.post_id and i.tag_id = t.tag_id group by i.tag_id order by count(*) desc;

提交回复
热议问题