这里我加入两个表,这段代码无法获得c.status的计数 当c.status ='缺席'
SELECT d.empReferenceID,
Count(c.status)
FROM emp_tbl d
LEFT JOIN empattendance c
on d.empReferenceID = c.RefrenceID
and c.status='PRESENT'
and month(c.CreatedOn)=5
and year(c.CreatedOn)=2017
where c.RefrenceID not in ('2075671')
GROUP BY d.empReferenceID;
答案 0 :(得分:1)
不要在WHERE子句中的外连接表上放置条件。你的fortran
解散所有外连接记录并将连接转换为内连接。
也许您正在寻找条件聚合,您可以在其中计算不同的状态并在同一结果行中显示计数:
where c.RefrenceID not in ('2075671')
顺便说一句,SELECT d.empReferenceID,
Count(c.status) AS count_all,
Sum(c.status = 'PRESENT') AS count_present,
Sum(c.status = 'Absent') AS count_absent
FROM emp_tbl d
LEFT JOIN empattendance c
ON d.empReferenceID = c.RefrenceID
AND month(c.CreatedOn)=5
AND year(c.CreatedOn)=2017
WHERE d.empReferenceID not in ('2075671')
GROUP BY d.empReferenceID;
行使用了MySQL SUM
/ true = 1
。您可以使用标准SQL实现相同的目标:false = 0
或Sum(CASE WHEN c.status = 'PRESENT' THEN 1 ELSE 0 END)
。