Mysql
 sql >> Database >  >> RDS >> Mysql

Generazione di una ripartizione oraria MySQL dal timestamp corrente

Puoi creare un hourly tabella ed esegui un LEFT JOIN

create table hourly
(
  /* hour is not a reserved keyword */
  hour smallint(2) not null default 0
);

insert into hourly values (0),(1).... until 24

SELECT 
  hourly.hour, 
  COALESCE(COUNT(activities),0) AS "No of Activities"
FROM hourly
LEFT JOIN tbl1 ON hourly.hour=hour(tbl1.TimeStamp)
WHERE 
  tbl1.timestamp>=DATE_SUB(NOW(), INTERVAL 24 HOUR) AND 
  tbl1.Name=? 
GROUP BY hourly.hour
ORDER BY hourly.hour;