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

后端 未结 5 1649
感情败类
感情败类 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:37

    First, you should store this using a junction table, with one row per post and tag. Sometimes, however, we cannot control the structure of data we are working with.

    You can do what you want assuming you have a list of valid tags:

    select vt.tag, count(t.postid) as cnt
    from validtags vt left join
         table t
         on find_in_set(vt.tag, t.tags) > 0
    group by vt.tag
    order by cnt desc;
    

提交回复
热议问题