有個time字段,想做個統計,統計每個時段的數據量,比如統計今天24個時段之內的每個時段數據量。
MYSQL命令:
SELECT FROM_UNIXTIME(`time`,'%Y%m%d%H') AS hh, COUNT(*) AS cnt FROM zz_sccess GROUP BY hh
time是時間字段
%Y%m%d%H是時間輸出格式
可以結合php使用
例如dedecms,php代碼:
//時段統計
$sqlsd="SELECT FROM_UNIXTIME(`time`,'%Y%m%d%H') AS hh, COUNT(*) AS cnt FROM js_sccess where time<$dayEnd AND time>$dayBegin GROUP BY hh";
$newsd = array();
$dsql->SetQuery($sqlsd);
$dsql->Execute();
while ($rowsd = $dsql->GetArray()) {
$newsd[] = $rowsd;
}
html模板代碼:
<?php
foreach ($newsd as $newsdlist)
{
?>
<tr class="odd gradeX">
<td ><?php
$sd=substr($newsdlist['hh'], -2);
$sdd=$sd+1;
echo $sd.':00-'.$sdd.':00';
?></td>
<td ><?php echo $newsdlist['cnt'];?></td>
</tr>
<?php
}
?>
*