查询每个经理的员工人数(经理也为零)

时间:2018-08-02 08:54:07

标签: mysql

经理表:

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 名员工包括在内?

1 个答案:

答案 0 :(得分:2)

您应该使用左联接

<ion-input>