order keywords by frequency in PHP mySql

故事扮演 提交于 2019-12-22 09:56:46

问题


I've got a database with video ids and N keywords for each video. I made a table with 1 video ID and 1 keyword ID in each row.

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

Is it possible to do that with sql or do I need to use php arrays?

Thanks


回答1:


I don't see the need for a join here. Simply list all the keywords along with the number of times the keyword appears, ordered from most frequent to less frequent.

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



回答2:


If I understand you correctly you can try

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


来源:https://stackoverflow.com/questions/2647111/order-keywords-by-frequency-in-php-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!