使用这两个查询。在while循环中的第二个,我能够在表das上显示为行和小时作为列,以逐个小时地获得活动计数。这里的问题是,没有按小时显示每小时的活动,这应该在第二个查询中。请检查我的查询,如果您有任何疑问,请告诉我。该脚本显示de信息,但不按小时(0到23)显示。
$query = "SELECT DISTINCT month(timestamp), day(timestamp) AS days
FROM security_transactions
where timestamp <= getdate()
order by month(timestamp), day(timestamp)";
$result = odbc_exec($conn,$query);
$number_day = odbc_result($result,'days');
$query2 = "SELECT COUNT(id) as hours
FROM security_transactions
WHERE MONTH(timestamp) = $month_count
AND DAY(timestamp) = $$number_day
GROUP BY DATEPART(hh, timestamp)";
这个想法是有这样的一行:
天| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 12等等......
1月1日452 | 458 | 53 | 584 | 125 | 056 | 57 | 48 | 589 | 410 |等等...答案 0 :(得分:1)
我认为你没有理由做两个单独的查询。我会这样做一个查询:
SELECT count(*) as countof, year(timestamp) as years, month(timestamp) as months, day(timestamp) as days, hour(timestamp) as hours
FROM security_transactions
WHERE timestamp <= getdate()
GROUP BY years, months, days, hours
每个组合都会得到一行,因此您的前端代码只需要检测年/月/日组合何时发生变化,这可以通过简单的循环完成。
好的,有关php循环(未测试)的一些帮助
$sql = '...query';
$data = array();
while ($row = mysql_fetch_assoc($result)) {
$daykey = $row['years'].'-'.$row['months'].'-'.$row['days'];
if (!array_key_exists($daykey, $data)) {
//prefill the hours
$data[$daykey] = array_fill(0, 23, 0);
}
$data[$daykey][$row['hours'] = $row['countof'];
}
// Now you have array already setup with day/hours counts. Also solves issue if hours don't have counts.
foreach ($data as $day => $hours) {
//Output your day markup here
foreach ($hours as $hour => $val) {
// Output one hour value
echo "$val |";
}
// close your day markup here (newline or whatever you're using)
}