简体   繁体   中英

mysql select sum of records every 10 minutes past 48 hours

I want to create an array of data with the sum of records in a mysql table. I need a sum of data from every 10 minutes from now to the last 48hours.

i have a working code in php, but is there a possibility to create this query directly in mysql?

for ($p=0;$i<=2880;$p+10){
$sql = "
select sum(rotations) 
  from wheel 
 where time BETWEEN (DATE_ADD(NOW() , INTERVAL - ".$p." MINUTE) AND (DATE_ADD(NOW() ,INTERVAL - ".$p+10." MINUTE)
"; 
}

Thank you in advance.

You can convert time to a numeric timestamp, then divide by the number of seconds in 10 minutes to group it.

SELECT FROM_UNIXTIME(600 * FLOOR(UNIX_TIMESTAMP(time)/600)) AS start_time, SUM(rotations)
FROM wheel
WHERE time >= DATE_SUB(NOW(), INTERVAL 2 DAY)
GROUP BY start_time

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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