我有一个查询,它对按日期分组的行进行求和。基本上,它计算一天内有多少会员申请,并逐日打印结果。当然,如果一天内没有应用程序,它就不会显示该行(15号和17号下面的示例没有应用程序……
Date | Applications
-----------------------------------
12/01/2010 | 44
13/01/2010 | 73
14/01/2010 | 66
16/01/2010 | 102
18/01/2010 | 12
19/01/2010 | 22我需要它打印出日期和0的天数时,没有申请,因此没有跳过的日子。你知道怎么做吗。我想过一年中的每一天都会加入一张桌子,但这似乎有点过分了。
查询如下
SELECT
application_date AS Date,
COUNT(*) AS Applications
FROM members
GROUP BY ap_date 发布于 2010-01-21 14:01:40
这与another question on SO非常相似。一般的共识似乎是:
在program.
最好的选择是#1 -它是最小的卷积,并且应该具有最低的开销。
发布于 2010-01-21 13:01:36
选择有问题的日期范围。然后遍历日期范围,查看当前行是否匹配。如果不是,则输出0。如果是,则输出适当的数字并前进到下一行。
发布于 2010-01-22 02:30:46
我将创建一个PHP数组,数组索引将是一个字符串,日期,我喜欢这种格式YYYY-MM-DD,我会这样做(注意键的日期格式很重要)
// how far in the past to go
$days_in_the_past = 365;
// default to today, unix timestamp file
$start_day = time();
// the array that contains all out dates
$dates = array();
// sec * min * hours
$secs_in_a_day = 60 * 60 * 24;
// note we're going backwards here
for ( $i=0; $i <= $days_in_the_past; $i++ )
{
$key = date('Y-M-D', ($start_day - ($secs_in_a_day * $i)));
// this sets the format of 2010-01-21 as the key
$dates[$key] = "";
}
$query = 'SELECT date, app FROM table';
$result = mysql_query($query);
while ( $row = mysql_fetch_assoc($result) )
{
$key = date('Y-M-D', strtotime($row['Date']));
$dates[] = $row['Applications'];
}如果您想要按顺序排列日期,只需对数组进行排序。
https://stackoverflow.com/questions/2107012
复制相似问题