我有问题。那么,我的表:
{{1}}
我想通过得分desc获得最后3行的每个级别。我试过这样:
{{1}}
但不行。你能帮我吗 ? Thx提前和抱歉我的英语
答案 0 :(得分:4)
有几种技术可以做到,一种方法是使用用户定义的变量
select id,`level`,score
from (
select *,
@rn:= if(@prev_level = `level`,@rn+1,1) as rn,
@prev_level:= `level`
from mytable,(select @rn:=0,@prev_level:=0)r
order by `level`,score desc
)r
where r.rn <=3;
答案 1 :(得分:-1)
SQL解决方案:
select * from table t1
WHERE
(
SELECT COUNT(*)
FROM table t
WHERE t.level = t1.level AND
t.score >= t1.score
) <= 3 order by t1.level, t1.score desc