Skip to content
Advertisement

Getting most popular tags [closed]

Writing a query that returns like the table below. What I am trying to do is get the popularity of tags based on the results returned.

JavaScript

Trying to write a query that will return tag popularity like this…

JavaScript

or even maybe just return from the initial query with just the tags and counts for each.

Advertisement

Answer

Your first effort should go into fixing your data model. Do not store multiple values in a delimited list.

You should have a separate table to store the relations between things and tags, with eac tuple on a different row.

Table things:

JavaScript

Table tags:

JavaScript

Table things_tags:

JavaScript

Then, couting the popularity of each tag is as simple as:

JavaScript

On the other hand, a design such as yours makes it much more complicated to maintain data integrity and manipulate the values. What you ask for would typically require a recursive query to split the csv strings into elements.

Recommended reading: Is storing a delimited list in a database column really that bad?.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement