我想列出BR,BRANCHNAME以及其中雇用的人数。总共有5个分支机构,其中只有4个分支机构在其中工作; 05分行没有员工。使用以下代码后,分支05将不会显示,因为where
语句后将不包括分支05的行。我想显示一行"05 Br05 0"
。
SELECT EMPLOYEE.BR, BRANCHNAME, Count(*) AS Number
FROM EMPLOYEE, BRANCH
WHERE (EMPLOYEE.BR = BRANCH.BR)
GROUP BY EMPLOYEE.BR, BRANCHNAME;
结果是:
BR BRANCHNAME Number
01 Br01 6
02 Br02 4
03 Br03 5
04 Br04 6
我希望得到以下结果:
BR BRANCHNAME Number
01 Br01 6
02 Br02 4
03 Br03 5
04 Br04 6
05 Br05 0
答案 0 :(得分:0)
看起来你想要一个LEFT JOIN
,即使没有匹配的员工,也会给出一个带有null结果的可数行。
由于您尚未添加表格结构,我认为branchname
是branch
表中的字段。
SELECT branch.br, branch.branchname, COUNT(employee.br) AS Number
FROM branch
LEFT JOIN employee
ON branch.br = employee.br
GROUP BY branch.br, branch.branchname
An SQLfiddle to test with (based on SQL Server since Access is not available)