我即将根据MySQL表的值生成一些统计信息。我希望在一年中的某个月和本月的前一天产生一些数字。
我当然可以手动完成所有这些,但这似乎不是一个好方法:) 所以任何对我如何生成这些统计数据有一些想法的人。
OBS。即使某个月没有任何MySQL记录,我也想获得一年中的所有月份。
奖金:我收到了一个小问题。提供统计数据的表格每周将获得约1000条记录。我的头脑,随着时间的推移似乎是一个糟糕的方法。任何建议采取更好方法的人都会受到欢迎。 我考虑过创建CSV文件。
提前感谢。非常感谢!
编辑:按照要求
+---------------+------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| member_id | int(4) | NO | | 0 | |
| status | tinyint(1) | NO | | 0 | |
| timestamp | timestamp | NO | | CURRENT_TIMESTAMP | |
+---------------+------------+------+-----+-------------------+----------------+
答案 0 :(得分:2)
这样的东西?
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
好的我已经有了它,但它未经测试,请记住PHP是我的第4或第5语言。我很确定这里的一些大师可以更优雅地做到这一点。
<?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);
?>