Mysql的兩種「超過多少次」寫法(力扣596)

題目:code

有一個courses 表 ,有: student (學生) 和 class (課程)。io

請列出全部超過或等於5名學生的課。class

例如,表:test

+---------+------------+
| student | class      |
+---------+------------+
| A       | Math       |
| B       | English    |
| C       | Math       |
| D       | Biology    |
| E       | Math       |
| F       | Computer   |
| G       | Math       |
| H       | Math       |
| I       | Math       |
+---------+------------+

應該輸出:效率

+---------+
| class   |
+---------+
| Math    |
+---------+

Note:
學生在每一個課中不該被重複計算。select

 

答案1:di

select a.class from
(select count(distinct student) as num, class from courses group by class) a
where a.num >= 5時間

答案2:
select class from courses group by class having count(class) > 4vi

對比一下效率是同樣的:co

[SQL] SELECT age from test_user GROUP BY age having count(age)>1;
受影響的行: 0
時間: 0.001s

[SQL]

select a.age from (select count(age) as num, age from test_user group by age) a where a.num >= 2;受影響的行: 0時間: 0.001s

相關文章
相關標籤/搜索