我有一个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,它只是按随机顺序对它们进行分组。
关于如何实现这一目标的任何想法?
答案 0 :(得分: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