0

我意识到这可能不是一个有效的选择,但它现在可以工作......主要是。我遇到的问题是,如果没有为“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
4

1 回答 1

0

COALESCE将返回提供的第一个非空值。

total.tminutes-COALESCE(indirect.iminutes, 0)

COALESCE当 iminutes 为 NULL 时,此处将返回 0。您可以对 dminutes 执行相同的操作。

您可以查看 MySQL 参考手册http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce

于 2012-10-29T20:51:00.930 回答