select distinct t.name from student t where t.name not in(select t.name from student t where t.score <= 80)
这种写法比较容易想到,先查至少有一门课程小于80,然后not in ,就是查询所有课程都大于80。但是比较冗余,也没啥技术含量,关键这种如果数据量多效率也不好,于是我们有了解法2。
select t.name from student t group by t.name having t.name not in(select t.name from student t where t.score <= 80)
一眼望去貌似和上面那种差别不大,只是加了一个分组而已? 但是我们发现,这种写法不需要加去重,因为已经分组了。总的来说,这种写法略优于解法1,但还是老毛病,子查询需要查询2次,效率不高,于是,我们有了解法3.
select t.name from student t group by t.name having count(t.score) = sum(case when t.score > 80 then 1 else 0 end)
咳咳,这种写法就比较炫技了,又是分组又是聚合函数的。但是也是一种思路。至于效率的话,和第二种半斤八两,只能说这个逼装的挺6。难道没有一种不需要子查询的么? 让我们看看解法4
select t.name from student t group by t.name having min(t.score) > 80
高手过招,一招制敌。用这句话形容解法4再合适不过了,抓住了题目的关键点,合理的使用min()聚合函数。应该是这题的最优解。
评论