2

我需要有一个大规模的选择语句或多个查询。我需要在一天中将数据分解为特定的时间范围。这个在第一个间隔中效果很好,但在那之后我很难过。如果我有多个查询,则我的“mysql_fetch_array”在语法方面有问题(使用类似'else'的东西并通过它们)。

SELECT 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( )
      AND TIME(submit_date) BETWEEN '06:00:00' and '07:59:59'       
      GROUP BY U.user_name
    LIMIT 0 , 30

我的 fetch 数组(我需要更多,但是如何组合它们?)

<?php
while($rows=mysql_fetch_array($result1)){

?>
4

2 回答 2

3

好的,鉴于您希望每隔 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>';
于 2012-10-23T10:01:31.057 回答
1

这是关于您的问题“如何组合提取数组”。为什么没有一个数组来存储来自您的 fetch 数组的所有值,如下所示。

<?php
$customArray = array();
while($rows=mysql_fetch_array($result1)){

    $customArray['field1'] = $row['field1'];
    $customArray['field2'] = $row['field2'];

}

//another fetch array

while($rows=mysql_fetch_array($result2)){

    $customArray['field1'] = $row['field1'];
    $customArray['field2'] = $row['field2'];

}

//now $customArray will have all the required values you need. 
//This is not a great option as it is making the logic expensive. 
//Are you going to use this in a cron job ?
?>
于 2012-10-22T15:37:15.363 回答