Mysql 单表查询各班级总分前三名

Mysql 单表查询各班级总分前三名,第1张

select class,total,name from (select *,ywsc+sxsc as total from st ORDER BY total DESC) b

where

not EXISTS(select * from (select *,ywsc+sxsc as total from st ORDER BY total DESC) c where c.class=b.class and b.total <c.total GROUP BY c.class HAVING COUNT(*)>2 )

ORDER BY b.class,b.total DESC

试运行下这段

select *

from(

SELECT

obj.id,

obj.score,

CASE

WHEN @rowtotal = obj.score THEN

@rownum

WHEN @rowtotal := obj.score THEN

@rownum :=@rownum + 1

WHEN @rowtotal = 0 THEN

@rownum :=@rownum + 1

END AS rownum

FROM

(

SELECT id,name,score

fromtest -- 表的名字

ORDER BY score DESC

)) AS obj,

(SELECT @rownum := 0 ,@rowtotal := NULL) r

) as no2

where rownum <4


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/8423297.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-16
下一篇 2023-04-16

发表评论

登录后才能评论

评论列表(0条)

保存