我正在尝试编写一个查询,它会显示工作小时的每月持续时间矩阵。我正试图做到这一点。
我现在有这个。
正确的输出应该是
我的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 ║
╚═════════╩═════════════════════╩═════════════════════╝谢谢!!!