前言:“至多”的对立面是“至少”。通常,采用“至多”问题中表述的技巧变体,就可以解决“至少”问题。当解决“至少”问题时,把他们换成“没有更少的”说法会更好理解。
问题(6):找到至少选择两门课程的学生。
)
问题(9):找到只教一门课程的教授。
)
)
)
5:回答有关“一些”或“所有”的问题
问题(12):找到选取所有课程的学生。
select sd.Tid,sd.Name,sd.Age from Student sd,StudentTakeCourses stc
where sd.Tid=stc.Sid
group by sd.Tid,sd.Name,sd.Age
having count(stc.Cid)=(select count(*) from course) --根据课程数排除课程没有都选的学生
where sd.Tid=stc.Sid
group by sd.Tid,sd.Name,sd.Age
having count(stc.Cid)=(select count(*) from course) --根据课程数排除课程没有都选的学生
问题(13):找到比其他所有学生都大的学生。(找到年龄最大的学生)
--使用max聚集函数
select * from Student where Age=(select max(Age) from Student)
select * from Student where Age=(select max(Age) from Student)
还有一种比较常见的方式:
--常见方式
select * from Student where Age>=all (select Age from Student)
select * from Student where Age>=all (select Age from Student)
最后通过自连接也可以查询到结果,不过没有上面两种看起来直接了当:
--自连接方式
select * from Student
where Age not in
(select sd1.Age from Student sd1,student sd2
where sd1.Age<sd2.Age
)
select * from Student
where Age not in
(select sd1.Age from Student sd1,student sd2
where sd1.Age<sd2.Age
)
<完>