博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

查询各科成绩最好的学生

Posted on 2017-07-14 17:15  和风细雨汪汪  阅读(359)  评论(0编辑  收藏  举报

course 

cr_id cr_name
11 语文
12 数学
13 英语

score 

cr_id st_id score

11 1 25
11 2 25
11 3 11
12 1 21
12 2 22
12 3 23
13 1 21
13 2 22
13 3 23

student 

st_id st_name
1 张
2 李
3 王

 

select * from score a where score in (SELECT MAX(score) from score s GROUP BY s.cr_id);


select * from score a where a.score = (SELECT MAX(score) from score s where s.cr_id=a.cr_id);


select b.cr_name,a.score,c.st_name from
(select * from score a where score in (SELECT MAX(score) from score s GROUP BY s.cr_id))a,
course b,
student c
where a.cr_id=b.cr_id and a.st_ID=c.st_id