Mysql 单表查询各班级总分前三名
答案:2 悬赏:80 手机版
解决时间 2021-04-07 10:25
- 提问者网友:我们很暧昧
- 2021-04-07 05:47
Mysql 单表查询各班级总分前三名
最佳答案
- 五星知识达人网友:詩光轨車
- 2021-04-07 07:08
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追问你已经做的很好了,但是测试结果还是有一条不符合,07-23班只查出了前2名。追答这是个bug如果是第一名有并列这个SQL还是有问题的追问07-23班是第二名有三个并列
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追问你已经做的很好了,但是测试结果还是有一条不符合,07-23班只查出了前2名。追答这是个bug如果是第一名有并列这个SQL还是有问题的追问07-23班是第二名有三个并列
全部回答
- 1楼网友:傲气稳了全场
- 2021-04-07 08:24
表结构给出来追问CREATE TABLE IF NOT EXISTS `st` (
`id` int(4) NOT NULL,
`name` varchar(20) NOT NULL,
`class` varchar(10) DEFAULT NULL,
`age` int(10) DEFAULT NULL,
`ywsc` int(30) DEFAULT NULL,
`sxsc` int(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;追答select class,ywsc+sxsc as total from baidu.st group by class order by total desc limit 1, 3追问总分不是单列,需要你做个相加运算,ywsc+sxsc。追答我知道已经你发给你了追问测试失败。是各个班级的前三名,前三名可不是只有三个人,总分相同要保持并列。你给的语句连全年级最高都没取出来。追答我再看看
`id` int(4) NOT NULL,
`name` varchar(20) NOT NULL,
`class` varchar(10) DEFAULT NULL,
`age` int(10) DEFAULT NULL,
`ywsc` int(30) DEFAULT NULL,
`sxsc` int(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;追答select class,ywsc+sxsc as total from baidu.st group by class order by total desc limit 1, 3追问总分不是单列,需要你做个相加运算,ywsc+sxsc。追答我知道已经你发给你了追问测试失败。是各个班级的前三名,前三名可不是只有三个人,总分相同要保持并列。你给的语句连全年级最高都没取出来。追答我再看看
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯