思路:采用逆向思维想想。求三门成绩都大于 80 的人,也可以是使先查出有成绩小于80 的人,再除去这些人不就是三门成绩都大于 80 的人了么?
具体办法:先扫描表,查出有成绩小于 80 的人的姓名,然后再次扫描表,用 not in 或 not exists 方法。
// not in
SELECT DISTINCT A.name FROM Student A
WHERE A.name not in(
SELECT Distinct S.name FROM Student S WHERE S.score <80)
// not exists
SELECT DISTINCT A.name From Student A
where not exists (SELECT 1 From Student S Where S.score <80 AND S.name =A.name)
取出外表第一条数据 ,然后与内表 根据连接条件,形成一条或多条数据,判断这些生成的数据中是否存在或者是不存在符合 where 条件的 。结果为 ture 的那条外表
记录旧被查询出来。
实例过程:取出外表的第一条记录,和内表通过姓名条件连接,这时候产生2两记录,根据 not exists 是判断不存在。 条件是 score < 80。
而这两条记录存在一条记录小于80,所以于 not exists 不符合,该条记录不被查出。
SELECT S.name
FROM Student S
GROUP BY S.name
Having MIN(S.score) >= 80
