我意识到这可能不是一个有效的选择,但它现在适用...大多数情况下。我遇到的问题是,如果没有为“iminutes”和“dminutes”提交值...我没有数据。我认为如果他们都返回值为“0”,它将解决这个问题,但不知道如何让他们这样做。建议?
SELECT user,total.tuser_id, total.tminutes,
total.tminutes-indirect.iminutes AS itminutes, total.tminutes-direct.dminutes AS dtminutes
FROM
(SELECT U.user_name AS user,U.user_id AS tuser_id, SUM(M.minutes) AS tminutes
From summary S
JOIN users U ON U.user_id = S.user_id
JOIN tasks TA ON TA.task_id = S.task_id
JOIN tcompleted TC ON TC.tcompleted_id = S.tcompleted_id
JOIN minutes M ON M.minutes_id = S.minutes_id
JOIN hour_interval H ON H.hourinterval_id = S.hourinterval_id
WHERE DATE(submit_date) = curdate()
AND TIME(submit_date) BETWEEN '00:00:01' and '23:59:59'
GROUP BY U.user_id) total
JOIN
(SELECT U.user_id AS iuser_id, SUM(M.minutes) AS iminutes
FROM summary S
JOIN users U
ON U.user_id = S.user_id
JOIN minutes M
ON M.minutes_id = S.minutes_id
JOIN tasks TA
ON TA.task_id = S.task_id
WHERE TA.task_type='indirect'
AND DATE(submit_date) = curdate()
AND TIME(submit_date) BETWEEN '00:00:01' and '23:59:59'
GROUP BY U.user_id) AS indirect
ON total.tuser_id = indirect.iuser_id
JOIN
(SELECT U.user_id AS duser_id, SUM(M.minutes) AS dminutes
FROM summary S
JOIN users U
ON U.user_id = S.user_id
JOIN minutes M
ON M.minutes_id = S.minutes_id
JOIN tasks TA
ON TA.task_id = S.task_id
WHERE TA.task_type='direct'
AND DATE(submit_date) = curdate()
AND TIME(submit_date) BETWEEN '00:00:01' and '23:59:59'
GROUP BY U.user_id) AS direct
ON total.tuser_id = direct.duser_id
ORDER BY total.tuser_id
答案 0 :(得分:0)
COALESCE
将返回提供的第一个非空值。
total.tminutes-COALESCE(indirect.iminutes, 0)
当iminutes为NULL时, COALESCE
这里将返回0。您可以在dminutes中执行相同的操作。
您可以查看MySQL参考手册http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce