好的,鉴于您希望每隔 2 小时定期获取数据,您可以尝试以下操作:
SELECT FLOOR(hour(S.submit_date)/2)*2, U.user_name,ROUND(SUM((TA.task_average*TC.completed)/60),2) AS equiv1, S.submit_date
SUM(TC.completed) AS ttasks1,
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
WHERE DATE(submit_date) = curdate( )
GROUP BY U.user_name, FLOOR(hour(S.submit_date)/2)
LIMIT 0 , 30
whereFLOOR(hour(S.submit_date)/2)*2
将每小时映射到每 2 小时的第一个(偶数)小时,您可以按此值分组。IE。
0, 1 -> 0
2, 3 -> 2
4, 5 -> 4
etc...
使用 php 更新包括:
一些注意事项:
- 我用过mysqli
- 我已经将原始查询与
hours
派生表结合在一起,以确保时间间隔中没有“间隙”(假设为 6:00 - 20:00)
- 我已经订购了
user
,所以当我们遍历结果时,我们可以打印给定用户的表格单元格,然后在用户更改时打印一个新的表格行。
这是代码:
echo '<table border=1><tr><td></td>';
for ($i=6; $i<=18; $i=$i+2) {
echo '<td colspan=2>'.$i.' - '.($i+2).'</td>';
}
$mysqli = new mysqli('MY_HOST', 'MY_USER', 'MY_PASSWORD', 'MY_DATABASE');
$sql = "
SELECT user_name, IFNULL(equiv1,0) AS equiv1, IFNULL(ttasks1,0) AS ttasks1, hour
FROM
(
SELECT 6 AS hour
UNION SELECT 8
UNION SELECT 10
UNION SELECT 12
UNION SELECT 14
UNION SELECT 16
UNION SELECT 18
) hours LEFT JOIN
(
SELECT FLOOR(hour(S.submit_date)/2)*2 as task_hour, U.user_name, ROUND(SUM((TA.task_average*TC.completed)/60),2) AS equiv1, S.submit_date
SUM(TC.completed) AS ttasks1
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
WHERE DATE(submit_date) = curdate( )
GROUP BY U.user_name, FLOOR(hour(S.submit_date)/2)
LIMIT 0 , 30
) task_summary
ON hours.hour = task_summary.task_hour
ORDER BY user_name, hour
";
$result = $mysqli->query($sql);
$user_name = '';
while ($row = $result->fetch_assoc()) {
if ($user_name <> $row['user_name']){
echo '</tr><tr><td>'.$row['user_name'].'</td>'; //start a new row if user changes
$user_name = $row['user_name']; //update user variable for checking on next iteration
}
echo '<td>'.$row['equiv1'].'</td>';
echo '<td>'.$row['ttasks1'].'</td>';
}
echo '</tr><table>';