我有一张表如下:
id datestart dateend hours
1 2014-07-21 2014-07-25 1.5
2 2014-07-28 2014-07-31 2
在MySQL中,如何查询是否希望每天(datestart到dateend)返回总和(小时),结果如下:
id daterange (datestart - dateend) totalhours(hours)
1 2014-07-21 2014-07-25 7.5
2 2014-07-28 2014-07-31 8
你可以看到每天等于几小时。
以下SQL并不是很有说服力。
select id, datediff (end, start) * `hours` as total_hours
from project_staff_assignment
它不会返回实际的小时数,也不会包含或计算开始日期和结束日期
答案 0 :(得分:0)
在您进行编辑后,似乎天数是从开始日到结束日期开始和结束的每一天。该声明应该:
SELECT
id,
datestart,
dateend,
-- because datediff won't include both, start and end, we'll add one day
datediff(dateend, datestart) + 1 as daterange,
(datediff (dateend, datestart) + 1) * `hours` as total_hours
FROM
project_staff_assignment
<强> DEMO 强>