gpt4 book ai didi

mysql - 如何找到选择最少学生的类(class)名称

转载 作者:行者123 更新时间:2023-11-29 04:14:55 25 4
gpt4 key购买 nike

有没有办法找到选择最少学生的类(class)名称?

非常感谢任何帮助。

SQL 模式:

Create table If Not Exists courses (student varchar(255), class varchar(255))
Truncate table courses
insert into courses (student, class) values ('A', 'Math')
insert into courses (student, class) values ('B', 'English')
insert into courses (student, class) values ('C', 'Math')
insert into courses (student, class) values ('D', 'Biology')
insert into courses (student, class) values ('E', 'Math')
insert into courses (student, class) values ('F', 'Computer')
insert into courses (student, class) values ('G', 'Math')
insert into courses (student, class) values ('H', 'Math')
insert into courses (student, class) values ('I', 'Math')

表格:

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

应该输出:

+---------+
| class |
+---------+
| English |
+---------+
| Biology |
+---------+
| Computer|
+---------+

最佳答案

您可以使用 HAVING 子句来完成此操作:

SELECT 
class
FROM
courses
GROUP BY
class
HAVING
COUNT(*)=(
SELECT
COUNT(*)
FROM
courses
GROUP BY
class
ORDER BY
COUNT(*) ASC
LIMIT 1
)

希望对您有所帮助,

关于mysql - 如何找到选择最少学生的类(class)名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55768431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com