Home  >  Q&A  >  body text

mysql 按天统计记录

突然想不想来这SQL应该怎么写

现在两字段
time 为时间戳
value 为某一笔交易金额
如何用sql查出所有的天的交易金额统计,得到类似下面的数据,如果某一天没有记录,那就为0,但也查询出来,如下面18号

day amount
2016-07-19 999
2016-07-18 0
2016-07-17 777
天蓬老师天蓬老师2714 days ago615

reply all(4)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-17 15:16:21

    "How to use SQL to find out the transaction amount statistics of all days"? ? What does it mean? Is it the total daily statistical amount?

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 15:16:21

    SELECT FROM_UNIXTIME(time),'%Y-%m-%d') AS day,SUM(value) AS amount FROM table GROUP BY day;
    
    未测试

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 15:16:21

    select FROM_UNIXTIME(time, '%Y-%m-%d') AS day, sum(value) AS amount from table group by day

    If you want to display the statistical date that does not exist, you need to implement it in the program

    reply
    0
  • PHPz

    PHPz2017-04-17 15:16:21

    SELECT
    YEAR(time ) year,
    MONTH (time )month,
    DAY(time )day,
    SUM(value )amount
    FROM
    TABLE
    WHERE

    reply
    0
  • Cancelreply