我想寫一個查詢,它會顯示工作小時的月持續時間矩陣。我正試圖做到這一點。MySql月度持續時間矩陣
我現在有這個。
正確的輸出應該是
我的SQL是
`SELECT WEEK(start) as weekId, ( SELECT SUM(TIME_TO_SEC(TIMEDIFF(stop, start))) FROM tbl_time_clock WHERE WEEKDAY(start)=0 ) AS Monday, ( SELECT SUM(TIME_TO_SEC(TIMEDIFF(stop, start))) FROM tbl_time_clock WHERE WEEKDAY(start)=1 ) AS Tuesday, ( SELECT SUM(TIME_TO_SEC(TIMEDIFF(stop, start))) FROM tbl_time_clock WHERE WEEKDAY(start)=2 ) AS Wednesday, ( SELECT SUM(TIME_TO_SEC(TIMEDIFF(stop, start))) FROM tbl_time_clock WHERE WEEKDAY(start)=3 ) AS Thursday, ( SELECT SUM(TIME_TO_SEC(TIMEDIFF(stop, start))) FROM tbl_time_clock WHERE WEEKDAY(start)=4 ) AS Friday, ( SELECT SUM(TIME_TO_SEC(TIMEDIFF(stop, start))) FROM tbl_time_clock WHERE WEEKDAY(start)=5 ) AS Saturday, ( SELECT SUM(TIME_TO_SEC(TIMEDIFF(stop, start))) FROM tbl_time_clock WHERE WEEKDAY(start)=6 ) AS Sunday FROM tbl_time_clock WHERE MONTH(start) = {$monthId} AND user_id = {$userId} GROUP BY WEEK(start)`
表看起來像
╔═════════╦═════════════════════╦═════════════════════╗ ║ user_id ║ start ║ stop ║ ╠═════════╬═════════════════════╬═════════════════════╣ ║ 1 ║ 2012-08-28 08:14:49 ║ 2012-08-28 10:14:49 ║ ║ 1 ║ 2012-08-25 10:00:32 ║ 2012-08-25 16:21:57 ║ ╚═════════╩═════════════════════╩═════════════════════╝
謝謝!
Thanks!決定與你的第二個解決方案。剛剛必須將內括號移到外面 - SUM(如果(WEEKDAY(start)= 0,TIME_TO_SEC(TIMEDIFF(stop,start)),0))如星期一 – waterloomatt
@glavić - 已經做到了;我認爲... – waterloomatt