gpt4 book ai didi

mysql 使用having 子句进行连接

转载 作者:行者123 更新时间:2023-11-29 13:15:47 25 4
gpt4 key购买 nike

这些是表格:

mysql> select * from professor;
+-------+--------+--------+--------+------+
| empid | name | status | salary | age |
+-------+--------+--------+--------+------+
| 1 | Arun | 1 | 2000 | 23 |
| 2 | Benoy | 0 | 3000 | 25 |
| 3 | Chacko | 1 | 1000 | 36 |
| 4 | Divin | 0 | 5000 | 32 |
| 5 | Edwin | 1 | 2500 | 55 |
| 7 | George | 0 | 1500 | 46 |
+-------+--------+--------+--------+------+
6 rows in set (0.00 sec)

mysql> select * from works;
+----------+-------+---------+
| courseid | empid | classid |
+----------+-------+---------+
| 1 | 1 | 10 |
| 2 | 2 | 9 |
| 3 | 3 | 8 |
| 4 | 4 | 10 |
| 5 | 5 | 9 |
| 6 | 1 | 9 |
| 2 | 3 | 10 |
| 2 | 1 | 7 |
| 2 | 4 | 6 |
| 2 | 5 | 2 |
| 4 | 2 | 6 |
+----------+-------+---------+
11 rows in set (0.00 sec)

mysql> select * from course;
+----------+------------+--------+
| courseid | coursename | points |
+----------+------------+--------+
| 1 | Maths | 100 |
| 2 | Science | 80 |
| 3 | English | 85 |
| 4 | Social | 90 |
| 5 | Malayalam | 99 |
| 6 | Arts | 40 |
| 7 | Biology | 100 |
+----------+------------+--------+
7 rows in set (0.00 sec)

问题是:

Return those courses that have been taught by at least 5 different employees

我的查询是:

select course.coursename from course
inner join works
on course.courseid=works.courseid
group by works.empid
having count(empid)>=5;

我应该将“科学”作为结果。但我得到 0 个结果。请帮忙。

最佳答案

试试这个

select course.coursename from course
inner join works
on course.courseid=works.courseid
group by course.coursename
having count(Distinct(works.empid))>=5;

关于mysql 使用having 子句进行连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21474651/

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