php - 基于MySQL和PHP的统计

标签 php mysql statistics

我将根据 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 |                |
+---------------+------------+------+-----+-------------------+----------------+

最佳答案

是这样的吗?

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 是我的第四或第五语言。我很确定这里的一些专家可以做得更优雅。

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

?>

关于php - 基于MySQL和PHP的统计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3776404/

相关文章:

machine-learning - 如何做出永远不会低估但可能高估的回归?

javascript - 中值 JS 函数。错误的

statistics - 使用朴素贝叶斯分类器进行在线学习

php - 构建数据库模式 Laravel4 与 Symfony2

php - 左连接三个 MySQL 表

Mysql 根据多列选择唯一记录并仅显示组和总金额

mysql - mysql中union和union all如何处理null?

php - Codeigniter $query->result() 返回奇怪的结果

PHP汉字无法插入到Mysql

iphone - 从 iOS 访问 Oracle