这是我的问题:
SELECT DISTINCT MAX(nb_played), id_person,id_list FROM `t_stats` ORDER BY MAX(nb_played) DESC;
我想返回为每个id列表播放的最大值nb,然后返回id人。
所以基本上,每个id列表的最佳nb_played,id_person。
无法解决这个问题。有谁可以帮助我?
答案 0 :(得分:1)
使用GROUP BY
查询:
SELECT t1.*
FROM t_stats t1
INNER JOIN
(
SELECT id_list, MAX(nb_played) AS max_nb_played
FROM t_stats
GROUP BY id_list
) t2
ON t1.id_list = t2.id_list AND t1.nb_played = t2.max_nb_played;
答案 1 :(得分:0)
您还可以使用相关子查询:
select s.*
from t_stats s
where s.nb_played = (select max(s2.nb_played)
from t_status s2
where s2.id_list = s.id_list
);
这可以利用t_status(id_list, nb_played)
上的索引,因此效果可能比使用group by
更好。