【问题标题】:Mysql SUM left join force show results with the sum of 0 zero resultsMysql SUM left join force 显示结果总和为 0 的结果为零
【发布时间】:2017-07-27 09:24:13
【问题描述】:

所以我现在为此苦苦挣扎了一段时间。

员工只需在表上添加开始和结束时间clocktimes

我只想要该日期间隔内所有员工的结果,包括零(未显示的那些)。我也尝试使用 COALESCE,但没有得到想要的结果。

regular 字段是已经以单位表示的时间量,例如:3.5。

我的问题是显示结果为 0(零)的那些,该任务中没有小时数。

使用的查询:

SELECT
employees.employeeid,
employees.emp_external_id,
employees.status_id,
employees.firstname,
employees.middlename,
employees.lastname,
IFNULL(SUM(clocktimes.Regular), 0) AS sumoftime
FROM
employees
LEFT JOIN clocktimes ON employees.emp_external_id  = clocktimes.EmployeeGUID
WHERE
employees.status_id <> 3 AND
date(clocktimes.StartDate) BETWEEN '2017-07-01' AND '2017-07-15'
GROUP BY
employees.employeeid
ORDER BY
employees.firstname ASC 

http://sqlfiddle.com/#!9/d89534/1

【问题讨论】:

标签: mysql left-join


【解决方案1】:
SELECT
employees.employeeid, 
employees.emp_external_id,
employees.status_id,
employees.firstname,
employees.middlename,
employees.lastname, 
IFNULL(SUM(clocktimes.Regular), 0) AS sumoftime
FROM
employees
LEFT JOIN clocktimes ON employees.emp_external_id = clocktimes.EmployeeGUID
WHERE
employees.status_id <> 3 AND
(date(clocktimes.StartDate) BETWEEN '2017-07-01' AND '2017-07-15') or date(clocktimes.StartDate) is null )
GROUP BY
employees.employeeid
ORDER BY
employees.firstname ASC

这里发生的事情是,由于您是通过日期过滤的,所以空列被过滤掉了。 检查 where 子句

【讨论】:

  • 是的,我知道带有日期的“where 子句”会限制结果。我只想包括“缺席”的人。我正在按照建议构建一个 sqlfiddle。
  • 你看到我在 where 子句中所做的改变了吗?
  • 对不起,我在旅行!您的回复解决了问题(缺少 '(' ) 但它有效。TY。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-10
  • 2012-11-17
  • 2020-11-04
  • 1970-01-01
  • 1970-01-01
  • 2012-07-07
相关资源
最近更新 更多