本文转自http://blog.sina.com.cn/s/blog_464f6dba0100orvf.html,所有权力归原作者所有。
例如有一个表student,其结构如下:
id
1
2
3
4
5
6
要求查询的结果如下:
id
3
2
4
6
顺序可调换,即为每个科目的最高分信息。(但是这里要注意,这里得到的最高分可能有多个)
SQL如下:
法一:
select student.id,student.name,student.sort,student.score from student inner join (select sort, max(score) as score from student group by sort) B on student.sort=B.sort AND student.score=B.score order by id
法二:
select * from student a where not exists(select * from student where a.score<score and a.sort=sort )
法三:
select * from student a where 1〉(select count(*) from student where a.score<score and a.sort=sort )
本文通过SQL查询实现,获取学生表中每个科目的最高分及其对应的学生信息,包括id、姓名、科目和分数。提供了三种不同的SQL方法来解决这个问题。
844

被折叠的 条评论
为什么被折叠?



