--work--
day hours
4 8
5 9
6 8
--crew--
employee day
2 4
3 4
6 4
2 5
3 5
4 5
--break--
hours day employee
1 4 2
.5 5 3
这是我的SQL查询:
SELECT w.hours, w.hours-b.hours, c.employee
FROM work w
LEFT JOIN crew c ON w.day = c.day
LEFT JOIN break b ON w.day = b.day
WHERE w.day = 4
每个员工减去1小时
我希望仅在该员工休息时才减去break.hours
。
如果他没有,我只想要work.hours
。
答案 0 :(得分:4)
COALESCE
就是你所需要的。
SELECT a.employee,
a.day,
b.hours - COALESCE(c.hours,0) totalHours
FROM crew a
INNER JOIN work b
ON a.day = b.day
LEFT JOIN `break` c
ON a.employee = c.employee AND
a.day = c.day
-- WHERE b.day = 4