一、建一张工具表只有hour一个字段 0-23,或者用mysql自带的mysql.help_topic筛选0-23
二、以0-23的那个为主表左关联,为Null设为0 ifnull(字段,0)
附sql
SELECT t.help_topic_id as hour, ifNULL(s.number, 0) AS number FROM mysql.help_topic t LEFT JOIN ( SELECT HOUR (r.capture_time) AS HOUR, count(*) AS number FROM md_capture_record r WHERE DATE_FORMAT(r.capture_time, '%y-%M-%d') = DATE_FORMAT(NOW(), '%y-%M-%d') GROUP BY HOUR ) s ON t.help_topic_id = s. HOUR where t.help_topic_id >=0 and t.help_topic_id<24; ORDER BY hour