gpt4 book ai didi

mysql - 查询统计每个经理的员工数量(经理的计数也为零)

转载 作者:行者123 更新时间:2023-11-29 06:40:55 27 4
gpt4 key购买 nike

经理表:

create table Manager(id integer, name varchar(100));
insert into Manager(id, name) values(1, "M1");
insert into Manager(id, name) values(2, "M2");
insert into Manager(id, name) values(3, "M3");

员工表以及经理 ID(m_id)

create table Employee(id integer, m_id integer, name varchar(100));
insert into Employee(id, m_id, name) values(11, 1,"E1");
insert into Employee(id, m_id, name) values(12, 1,"E2");
insert into Employee(id, m_id, name) values(13, 2,"E3");

执行的查询:

select M.id, M.name, count(E.id) from Manager as M, Employee as E where E.m_id=M.id group by E.m_id, M.id, M.name;

输出:

1   M1  2
2 M2 1

预期输出:

1   M1  2
2 M2 1
3 M3 0

请帮助我如何将 M3 经理以及分配给他的 0 员工包括在内?

最佳答案

您应该使用左连接

SELECT M.id, M.name, count(E.id) from Manager as M LEFT JOIN Employee as E ON E.m_id=M.id GROUP BY E.m_id, M.id, M.name;

关于mysql - 查询统计每个经理的员工数量(经理的计数也为零),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51649486/

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