SQL按计数sorting

如果我有一个表和这样的数据:

ID | Name | Group 1 Apple A 2 Boy A 3 Cat B 4 Dog C 5 Elep C 6 Fish C 

我希望按照从小到大的顺序排列,如:A-2条logging,B-1条logging,C-3条logging,所以会变成:

 3 Cat B 1 Apple A 2 Boy A 4 Dog C 5 Elep C 6 Fish C 

我试过了

  $sql = "SELECT ID,Name FROM table ORDER BY COUNT(Group)"; 

但它只是为我返回一个结果。

有什么提示吗? 谢谢。

您需要首先汇总数据,这可以使用GROUP BY子句完成:

 SELECT Group, COUNT(*) FROM table GROUP BY Group ORDER BY COUNT(*) DESC 

DESC关键字允许您先显示最高计数,默认顺序是ORDER BY,按升序显示最低计数。

试试:

 SELECT count(*),group FROM table GROUP BY group ORDER BY group 

按次序降序做

 SELECT count(*),group FROM table GROUP BY group ORDER BY count(*) DESC 

这将按返回group和列的group列返回结果,并按group顺序返回订单

 SELECT * FROM table group by `Group` ORDER BY COUNT(Group) 
 SELECT Group, COUNT(*) FROM table GROUP BY Group ORDER BY Group 

或者按点数sorting

 SELECT Group, COUNT(*) AS count FROM table GROUP BY Group ORDER BY count DESC 

尝试使用下面的查询:

 SELECT GROUP, COUNT(*) AS Total_Count FROM TABLE GROUP BY GROUP ORDER BY Total_Count DESC 

下面给我的是你所拥有的。 (通知组列)

 SELECT * FROM myTable GROUP BY Group_value, ID ORDER BY count(Group_value) 

让我知道这对你是否可以…

我想要得到你想要的东西…