gpt4 book ai didi

mysql - 查找至少有 2 名员工的部门

转载 作者:行者123 更新时间:2023-11-29 01:35:41 24 4
gpt4 key购买 nike

我需要生成 sql 问题,该问题将显示所有部门中至少有 2 个人。

SELECT department.name
FROM department
INNER JOIN employee
ON department.id = employee.department_id
GROUP BY employee.id
HAVING COUNT(employee.id) >= 2;

它没有向我显示此查询的任何内容

最佳答案

我认为您在错误的列上使用了 GROUP BY
如果你想统计每个部门的员工,那么你需要GROUP BY department.id

SELECT department.id, department.name, COUNT(employee.id) AS total_employee
FROM department
INNER JOIN employee
ON department.id = employee.department_id
GROUP BY department.id, department.name
HAVING COUNT(employee.id) >= 2;

关于mysql - 查找至少有 2 名员工的部门,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46969219/

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