像这样的事情?
select count(status) as total, year(timestamp) as yr, month(timestamp) as mnth from mytable group by yr,mnth
至于你的奖金问题,1000个记录一周没有那么多。如何切换到CSV文件帮助?您每周仍会获得1000条记录。
编辑
select count(status) as total, year(timestamp) as yr, month(timestamp) as mnth, day(timestamp) as dy from mytable group by yr,mnth,dy
编辑2
select count(status) as total, year(timestamp) as yr, month(timestamp) as mnth, day(timestamp) as dy, to_days(timestamp) daynum from mytable group by yr,mnth,dy
我添加了一个TO_DAYS领域,这将有助于你发现丢失的天,你通过扫描结果,daynum在应该是连续的。
编辑3
OK我已经在一搏,但它是未经测试并铭记PHP是我的第四或第五的语言。我很确定这里的一些大师可以更优雅地做到这一点。
<?php
$con = mysql_connect("myhost","myusername","mypassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydatabase", $con);
$result = mysql_query("select count(status) as total, year(timestamp) as yr, month(timestamp) as mnth, day(timestamp) as dy, to_days(timestamp) as daynum from mytable group by yr,mnth,dy");
$row = mysql_fetch_array($result);
$counter=$row['daynum']-$row['day']+1; // set up the daynum counter an initiaise to the first day of the month "-$row['day']+1"
//print out any blank rows at the beginning of the month
for ($i = $counter; $i <=$row['daynum'] ; $i++) {
echo "A blank row";
}
// start to loop through the result set
$finished=false;
do {
if($counter=$row['daynum']){ // if the daynumber of the row matches the counter then print the row and get the next row
echo "an output row from db".$row('dy')."-".$row('mnth')."-".$row('yr')."-----".$row('total');
$lastday=$row['dy'];
$lastmonth=$row['mnth'];
$lastyear=$row['yr'];
$row = mysql_fetch_array($result);
if (!$row) finished=true;
} else { // if the counter if not equal it must be less than $row['daynum'] so print blank rows and increment counter until it matches the current row.
$mytime = $counter*24*60*60; //convert days to seconds, because PHP doesn't seem to have a from_days function
$mydate = strftime("%Y-%m-%d", $mytime); //convert seconds to date
echo $mydate."a blank row"
$counter=$counter+1;
}
} while (! finished);
// print out any blank days at the end of the month
$daysinmonth = cal_days_in_month(CAL_GREGORIAN, $lastmnth, $lastyear);
for ($i = ($lastday+1); $i <=$daysinmonth; $i++) {
echo $i."-".$lastmonth."-".$lastyear." --- A blank row";
}
mysql_close($con);
?>
你能提供一些关于你的问题的更多细节,比如表定义吗? – Jaydee 2010-09-23 08:07:14