SELECT ww.*
FROM
( SELECT Weight, Winner, COUNT(*) AS Wins
FROM results
GROUP BY Weight, Winner
) AS ww
JOIN
( SELECT DISTINCT Weight
FROM results
) AS dw
ON ww.Weight = dw.Weight
AND ww.Winner =
( SELECT r.Winner
FROM results AS r
WHERE r.Weight = dw.Weight
GROUP BY r.Winner
ORDER BY COUNT(*) DESC
LIMIT 1
) ;
上的索引(Weight, Winner)
将有助于查询的效率。
你也可以通过这个查询得到正确的结果,但我不推荐它,因为它使用了 MySQL 的一些非标准特性和行为,因此将来可能会中断:
SELECT Weight, Winner, Wins
FROM
( SELECT Weight, Winner, COUNT(*) AS Wins
FROM results
GROUP BY Weight, Winner
ORDER BY Weight, Wins DESC
) AS ww
GROUP BY Weight ;