简体   繁体   English

MySQL PHP组按天和每天总计

[英]MySQL PHP group by day and total for each day

I have a table in a mysql database with 3 columns: id, value and tstamp. 我在mysql数据库中有一个包含3列的表:id,value和tstamp。 My tstamp column is in TIMESTAMP format, ie: 2011-01-21 08:32:22 我的tstamp列采用TIMESTAMP格式,即:2011-01-21 08:32:22

What i hope to achieve is use PHP to display a break down of a total for each day. 我希望实现的是使用PHP来显示每天总计的细分。 I cant think of a way to query the table, in pseudo code: select * from table group by day(part of tstamp) and add all numbers together from the value column for each day" 我想不出一种查询表的方法,用伪代码:从表组中选择*按天(tstamp的一部分)并从每天的值列中添加所有数字“

Is this going to be possible? 这有可能吗?

Thanks alot 非常感谢

EDIT: 编辑:

I have this query in combo with the accepted answer's code: 我将这个查询与接受的答案代码组合在一起:

$sql = "select date(tstamp), sum(".$column.") from mash group by date(tstamp)";
           $result = mysql_query($sql);
                while($row = mysql_fetch_array($result)){
                      $strXML .= "<set name='".date("G:i:s", strtotime($row["date"])).  "' value='".$row["sum"]."' color='AFD8F8' />";
                }

How do i access the array values in $row correctly? 如何正确访问$ row中的数组值?

If you want MySQL to return you the value you are looking for in ONE QUERY you can use: 如果您希望MySQL在ONE QUERY中返回您要查找的值,您可以使用:

select date(tstamp), sum(value)
  from your_table
 group by date(tstamp);

How about using this 怎么用这个

DATE_FORMAT(datestamp , '%Y') as year //for grouping over year
DATE_FORMAT(datestamp , '%Y-%m') as month //for grouping over month
DATE_FORMAT(datestamp , '%Y-%m-%d') as day //for grouping over day

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM