1

我有一个 MySQL 表,设置如下:

id  |  game_type | votes  
01  |  aaa       |  2  
02  |  bbb       |  2  
03  |  ccc       |  4  
04  |  aaa       |  7  
05  |  aaa       |  1  
06  |  ccc       |  11  

我想返回所有行,所以它们按game_type排序,但游戏类型的出现次数最多按降序排列,然后按投票数量按降序排列,如下所示:

04 aaa  7  
01 aaa  2  
05 aaa  1  
06 ccc  11  
03 ccc  4  
02 bbb  2

如果我只是在查询末尾有 ORDER BY,它只是按随机顺序对它们进行分组。

我怎么能做到这一点?

4

1 回答 1

1

试试这个:

SELECT M.id, M.game_type, M.votes 
FROM MyTable AS M INNER JOIN
    (SELECT game_type, COUNT(game_Type) AS TypeCount
     FROM MyTable
     GROUP BY game_type
     ORDER BY COUNT(game_Type) DESC) AS U
  ON M.game_type=U.game_type
ORDER BY U.TypeCount DESC, M.game_type, M.votes DESC
于 2012-07-04T19:19:14.400 回答