Sorting MYSQL Tag table

后端 未结 2 1346
迷失自我
迷失自我 2021-01-06 23:55

just wondering if it is possible to get the top 10 COUNT results and ordering by COUNT and alphabetically?

I have the following tables,

tags
-------
         


        
2条回答
  •  执念已碎
    2021-01-07 00:32

    Does the query work? If yes, you could use LIMIT 0, 10 to get only the top 10 rows.

    SELECT tag.*, COUNT(td.tag_ID) AS tagcount
    FROM Tagged td
    LEFT JOIN gen_Tags tag ON td.tag_ID = tag.tag_ID
    GROUP BY td.tag_ID
    ORDER BY tagcount DESC, tag.tag_Title ASC LIMIT 0, 10
    

    Another thing you might be interested in, is ranking. See here: http://www.fromdual.com/ranking-mysql-results

    Edit

    Maybe a subquery does what you want:

    SELECT list.* FROM (
      SELECT tag.*, COUNT(td.tag_ID) AS tagcount, 
      FROM Tagged td
      LEFT JOIN gen_Tags tag ON td.tag_ID = tag.tag_ID
      GROUP BY td.tag_ID
      ORDER BY tagcount DESC LIMIT 0, 10
    ) AS list ORDER BY list.tag_Title ASC, list.tagcount DESC
    

提交回复
热议问题