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提供された最初の非 null 値を返します。

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

COALESCEiminutes が NULL の場合、here は 0 を返します。dminutes についても同じことができます。

MySQL リファレンス マニュアルhttp://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesceを確認してください。

于 2012-10-29T20:51:00.930 に答える