2010-09-23 65 views
1

我即将根据MySQL表的值生成一些统计信息。我想在每个月的每个月和每个月的日期生成一些数字。基于MySQL和PHP的统计信息

我当然可以做所有这些手动,但这似乎并不是一个好方法:) 因此,任何人对我如何产生这些统计有一些想法。

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

你能提供一些关于你的问题的更多细节,比如表定义吗? – Jaydee 2010-09-23 08:07:14

回答

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

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); 

?> 
+0

这并没有解决需要错过几个月,也没有每月的几天 – Cez 2010-09-23 09:10:22

+0

正如Cez所说,这并不能真正解决我的问题。你建议的解决方案其实非常像我目前使用的解决方案。 – nickifrandsen 2010-09-23 09:28:57

+0

奖励问题:我只是有一个简单的想法,如果我按月对记录进行分组,例如在CSV文件中。加班我可以直接调用我需要的CSV文件而不是整个表格? 我可能不是个好主意。就像我说的那只是我的一个想法。 – nickifrandsen 2010-09-23 09:32:36