正如
@Strawberry所建议的那样,我最终用php而不是mysql编写了一个解决方案.对于任何感兴趣的人,这是代码.
此解决方案仅在空白日期之间的1天内返回0,因为这就是我所需要的.例:
> 2017年5月11日= 1
> 2017年5月16日= 0
> 2017年5月17日= 1
> 2017年5月19日= 0
> 2017年5月20日= 2
这让我可以像这样显示一个趋势线图:
$sql = 'SELECT DATE(poll_vote.date_insert) AS date_insert, COUNT(poll_vote.id_vote) AS q FROM poll_vote WHERE poll_vote.id_poll = :id_poll GROUP BY DATE(date_insert) ORDER BY date_insert';
$stmt = cnn()->prepare($sql);
if($id_poll) $stmt->bindValue(':id_poll', $id_poll, PDO::PARAM_INT);
$stmt->execute();
$data = $stmt->fetchAll();
# insert blank dates between existing dates
$hotness = array();
foreach($data as $item) {
if(!$temp) {
$temp = $item['date_insert'];
} else {
$date_past = new DateTime($temp);
$date_now = new DateTime($item['date_insert']);
$diff = $date_now->diff($date_past)->format("%a");
if($diff > 1) {
$date_new = new DateTime($item['date_insert']);
$date_new->modify('-1 day');
$hotness[] = array(
'date_insert' => $date_new->format('Y-m-d'),
'q' => 0
);
}
}
$hotness[] = array(
'date_insert' => $item['date_insert'],
'q' => $item['q']
);
}
# final result
print_r($hotness);