你有3種選擇:
- 嘗試迭代應用程序邏輯的日期(PHP)
- 產生充滿了你需要的日期的(臨時)表,並留下了它
- 加盟用mysql存儲過程溶液等in this answer
實施例爲應用邏輯實現:
<?php
date_default_timezone_set('Europe/Paris');
$startdate = strtotime('2011-11-01 00:00:01');
$days = 60;
$found_data = array(// this is generated by 1 mysql query
array('date_field' => '2011-11-02', 'count' => 5),
array('date_field' => '2011-11-03', 'count' => 1),
array('date_field' => '2011-11-04', 'count' => 6),
array('date_field' => '2011-11-08', 'count' => 9),
array('date_field' => '2011-11-09', 'count' => 3),
array('date_field' => '2011-11-10', 'count' => 5),
array('date_field' => '2011-11-12', 'count' => 1),
array('date_field' => '2011-11-15', 'count' => 1),
array('date_field' => '2011-11-18', 'count' => 4),
array('date_field' => '2011-11-21', 'count' => 9),
array('date_field' => '2011-11-23', 'count' => 1),
array('date_field' => '2011-11-28', 'count' => 8),
array('date_field' => '2011-11-30', 'count' => 6),
);
foreach ($found_data as $counts) { // we convert the results to a usable form, you can do this in the query, too
$count_info[$counts['date_field']] = $counts['count'];
}
for ($i = 0; $i <= $days; $i++) {
$date = date('Y-m-d', $startdate+$i*60*60*24);
printf("%s\t%s\n", $date, array_key_exists($date, $count_info) ? $count_info[$date] : 0);
}
?>
顯然,如果表中沒有該日期的命中,則選擇將不會返回任何內容 –