It would be a lot easier if you normalized the database and created a separate tag table. For example, if you have a database like this:
article
article_id
article_title
article_content
article_tag
tag_name
article_id
UNIQUE INDEX (tag_name, article_id)
UNIQUE INDEX (article_id, tag_name) # in order to perform fast lookups in both directions
(This could possibly be normalized even further by creating a tag
-table that contains the tag_name
and a tag_id
, and replacing tag_name
with tag_id
in article_tag
.)
Now you can present a list of the most popular tags using a query such as:
SELECT tag_name, count(article_id) c
FROM article_tag
GROUP BY tag_name
ORDER BY c DESC
LIMIT 10