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.
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;