学生信息表为 stu
id(学号) name(姓名) sex(性别) age(年龄)
学生成绩表为result
id(学号) pid(课程号) score(成绩)
select id,name,sex,age
from stu
where id = (select id from result as a,result as b
where (a.id = b.id)
and (a.pid = "001" and a.score >b.score));
可以用内连接查询如下(更好理解但是代码量相对比较大):
select id,name,sex,age
from stu t1
inner join
(select id from
(select id,score from result where pid = "001") a
inner join
(select id,score from result where pid = "002") b
on a.id = b.id and a.score > b.score) t2
on t1.id = t2.id;