Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Gerando um detalhamento por hora do MySQL a partir do timestamp atual


Você pode criar um hourly table e faça um 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;