mysql查询从每小时到每小时2小时

时间:2013-05-03 07:40:12

标签: mysql

如何更改下面的查询以获得2小时而不是1

SELECT DATE_FORMAT(x.time,'%Y-%m-%d %H:00:00')
     , avg(Ph) avg_Ph
  FROM Ph x where time >= NOW() - INTERVAL 1 DAY
 GROUP 
    BY DATE_FORMAT(x.time,'%Y-%m-%d %H:00:00');

1 个答案:

答案 0 :(得分:1)

SELECT DATE_FORMAT(FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(x.time)/7200)*7200),'%Y-%m-%d %H:00:00') AS `two_hour`
     , avg(Ph) avg_Ph
  FROM Ph x where time >= NOW() - INTERVAL 1 DAY
 GROUP 
    BY `two_hour`