Sort keywords by frequency in PHP mySql

I have a database with video IDs and N keywords for each video. I made a table with 1 video ID and 1 keyword in each row.

What is the easiest way to arrange keywords by frequency? I want to extract the number of times a keyword is used and order them.

Is it possible to do this using sql or do I need to use php arrays?

thank

+3
source share
2 answers

I do not see the need to join here. Just list all the keywords along with the number of times the keyword appears, from the usual to the less frequent.

SELECT keyword, COUNT(*) freq 
FROM keywordTable 
GROUP BY keyword 
ORDER BY freq DESC
+4
source

,

SELECT  VideoID,
        KeyWordID,
        COUNT(KeyWordID) Total
FROM    VideoKeywords
GROUP BY VideoID,
        KeyWordID
ORDER BY VideoID,Total DESC
0

Source: https://habr.com/ru/post/1741315/


All Articles