mySQL计数并从表中获得最大值

时间:2013-07-18 08:24:35

标签: mysql count max

我有一个简单的问题。我有这个表结构http://sqlfiddle.com/#!2/61cee/3/0,我只想计算每个玩家的得分是攻击者,然后从所有玩家和他的身份中选出最高分。我试过这个:

SELECT MAX(counts), id_player FROM 
  (SELECT COUNT(score) AS counts, id_player
   FROM fl_poll 
   WHERE position = 'attacker' 
   GROUP BY id_player) as maxi

但结果是错误的id。我犯了哪个错误?谢谢你的进步。

1 个答案:

答案 0 :(得分:2)

   SELECT COUNT(score) AS counts, id_player
   FROM fl_poll 
   WHERE position = 'attacker' 
   GROUP BY id_player
   ORDER BY COUNT(score) desc
   LIMIT 1