试图吸引所有用户,无论他们是否有任何时间?我的LEFT OUTER JOIN仍无法正常工作..
SELECT tblleaverequest.lqUser,
SUM(CASE WHEN DATE_FORMAT(tblleaverequest.lqStartDate, '%m') = 1 THEN (tblleaverequest.lqHoursPL) ELSE 0 END) AS 'Jan',
SUM(CASE WHEN DATE_FORMAT(tblleaverequest.lqStartDate, '%m') = 2 THEN (tblleaverequest.lqHoursPL) ELSE 0 END) AS 'Feb',
SUM(CASE WHEN DATE_FORMAT(tblleaverequest.lqStartDate, '%m') = 3 THEN (tblleaverequest.lqHoursPL) ELSE 0 END) AS 'Mar',
SUM(CASE WHEN DATE_FORMAT(tblleaverequest.lqStartDate, '%m') = 4 THEN (tblleaverequest.lqHoursPL) ELSE 0 END) AS 'Apr',
SUM(CASE WHEN DATE_FORMAT(tblleaverequest.lqStartDate, '%m') = 5 THEN (tblleaverequest.lqHoursPL) ELSE 0 END) AS 'May',
SUM(CASE WHEN DATE_FORMAT(tblleaverequest.lqStartDate, '%m') = 6 THEN (tblleaverequest.lqHoursPL) ELSE 0 END) AS 'Jun',
SUM(CASE WHEN DATE_FORMAT(tblleaverequest.lqStartDate, '%m') = 7 THEN (tblleaverequest.lqHoursPL) ELSE 0 END) AS 'Jul',
SUM(CASE WHEN DATE_FORMAT(tblleaverequest.lqStartDate, '%m') = 8 THEN (tblleaverequest.lqHoursPL) ELSE 0 END) AS 'Aug',
SUM(CASE WHEN DATE_FORMAT(tblleaverequest.lqStartDate, '%m') = 9 THEN (tblleaverequest.lqHoursPL) ELSE 0 END) AS 'Sep',
SUM(CASE WHEN DATE_FORMAT(tblleaverequest.lqStartDate, '%m') = 10 THEN (tblleaverequest.lqHoursPL) ELSE 0 END) AS 'Oct',
SUM(CASE WHEN DATE_FORMAT(tblleaverequest.lqStartDate, '%m') = 11 THEN (tblleaverequest.lqHoursPL) ELSE 0 END) AS 'Nov',
SUM(CASE WHEN DATE_FORMAT(tblleaverequest.lqStartDate, '%m') = 12 THEN (tblleaverequest.lqHoursPL) ELSE 0 END) AS 'Dec',
SUM(tblleaverequest.lqHoursPL) AS PaidLeaveTotal
FROM tblusers LEFT OUTER JOIN tblleaverequest ON tblleaverequest.lqUser = tblusers.username
WHERE (tblusers.clr_accrual = '1') AND (YEAR(tblleaverequest.lqStartDate) = YEAR(CURDATE()))
GROUP BY tblusers.username
答案 0 :(得分:3)
不确定您的数据库架构,但似乎您可以将一个条件从WHERE
移到ON
子句
SELECT tblusers.username,
SUM(CASE WHEN DATE_FORMAT(tblleaverequest.lqStartDate, '%m') = 1 THEN (tblleaverequest.lqHoursPL) ELSE 0 END) AS 'Jan',
SUM(CASE WHEN DATE_FORMAT(tblleaverequest.lqStartDate, '%m') = 2 THEN (tblleaverequest.lqHoursPL) ELSE 0 END) AS 'Feb',
SUM(CASE WHEN DATE_FORMAT(tblleaverequest.lqStartDate, '%m') = 3 THEN (tblleaverequest.lqHoursPL) ELSE 0 END) AS 'Mar',
SUM(CASE WHEN DATE_FORMAT(tblleaverequest.lqStartDate, '%m') = 4 THEN (tblleaverequest.lqHoursPL) ELSE 0 END) AS 'Apr',
SUM(CASE WHEN DATE_FORMAT(tblleaverequest.lqStartDate, '%m') = 5 THEN (tblleaverequest.lqHoursPL) ELSE 0 END) AS 'May',
SUM(CASE WHEN DATE_FORMAT(tblleaverequest.lqStartDate, '%m') = 6 THEN (tblleaverequest.lqHoursPL) ELSE 0 END) AS 'Jun',
SUM(CASE WHEN DATE_FORMAT(tblleaverequest.lqStartDate, '%m') = 7 THEN (tblleaverequest.lqHoursPL) ELSE 0 END) AS 'Jul',
SUM(CASE WHEN DATE_FORMAT(tblleaverequest.lqStartDate, '%m') = 8 THEN (tblleaverequest.lqHoursPL) ELSE 0 END) AS 'Aug',
SUM(CASE WHEN DATE_FORMAT(tblleaverequest.lqStartDate, '%m') = 9 THEN (tblleaverequest.lqHoursPL) ELSE 0 END) AS 'Sep',
SUM(CASE WHEN DATE_FORMAT(tblleaverequest.lqStartDate, '%m') = 10 THEN (tblleaverequest.lqHoursPL) ELSE 0 END) AS 'Oct',
SUM(CASE WHEN DATE_FORMAT(tblleaverequest.lqStartDate, '%m') = 11 THEN (tblleaverequest.lqHoursPL) ELSE 0 END) AS 'Nov',
SUM(CASE WHEN DATE_FORMAT(tblleaverequest.lqStartDate, '%m') = 12 THEN (tblleaverequest.lqHoursPL) ELSE 0 END) AS 'Dec',
SUM(tblleaverequest.lqHoursPL) AS PaidLeaveTotal
FROM tblusers
LEFT OUTER JOIN tblleaverequest
ON tblleaverequest.lqUser = tblusers.username
AND (YEAR(tblleaverequest.lqStartDate) = YEAR(CURDATE()))
WHERE (tblusers.clr_accrual = '1')
GROUP BY tblusers.username
答案 1 :(得分:1)
WHERE ... (YEAR(tblleaverequest.lqStartDate) = YEAR(CURDATE()))
如果记录tblLeaveRequest
中没有匹配项,则日期为null
。由于null
不能等于任何内容,因此这些记录将被删除。所以你基本上是将OUTER JOIN转换为INNER JOIN。将该比较移动到JOIN语句以保留OUTER JOIN。
<强>更新强>
请务必从tblusers
获取用户名,因为tblleaverequest.lqUser
值可能因OUTER JOIN而为空。
SELECT tblusers.username
, SUM(CASE ......)) AS Jan
, ....