按计数查询顺序

时间:2012-01-19 10:59:24

标签: mysql count sql-order-by

ID名称

  • 1 Asd
  • 2 Qwe
  • 3 Asd
  • 4 Asd
  • 5 Qwe
  • 6 Dsa

我需要一个查询按计数返回组顺序;

ID名称

  • 1 Asd
  • 2 Qwe
  • 3 Dsa

3 个答案:

答案 0 :(得分:2)

试试这可能会有帮助,

SELECT COUNT(Name) AS total, id, Name
FROM table_name
GROUP BY Name   
ORDER BY COUNT(Name) DESC;

答案 1 :(得分:1)

SELECT @rownum:=@rownum+1  AS ID,
       NameList.iName
FROM 
      (SELECT DISTINCT iName, COUNT(iName) AS iCount 
       FROM people 
       GROUP BY iName 
       ORDER BY iCount DESC) NameList, 
      (SELECT @rownum:=0) r 

更改此子查询

      (SELECT DISTINCT iName, COUNT(iName) AS iCount 
       FROM people 
       GROUP BY iName 
       ORDER BY iCount DESC)

到包含名称列表的表格。

答案 2 :(得分:0)

检查此

SELECT id, Name FROM user
GROUP BY Name   
ORDER BY COUNT(Name) DESC;