mysql統計前24小時數據沒有補0

SELECT
   t1. HOUR HOUR,
   COUNT(t2. HOUR) count
FROM
   (
      SELECT
         DATE_FORMAT(
            @cdate := DATE_ADD(@cdate, INTERVAL - 1 HOUR),
            '%y-%m-%d %H'
         ) HOUR
      FROM
         (
            SELECT
               @cdate := DATE_ADD(
                  DATE_FORMAT(NOW(), '%y-%m-%d %H'),
                  INTERVAL + 1 HOUR
               )
            FROM
               city
         ) t0
      LIMIT 24
   ) t1
LEFT JOIN (
   SELECT
      DATE_FORMAT(visit_time, '%y-%m-%d %H') HOUR
   FROM
      visit_log
   WHERE
      visit_time >= (NOW() - INTERVAL 24 HOUR)
) t2 ON t1. HOUR = t2. HOUR
GROUP BY
   t1. HOUR
ORDER BY
   t1. HOUR DESC

查詢當前時間前24小時日期天數html

 

      SELECT
         DATE_FORMAT(
            @cdate := DATE_ADD(@cdate, INTERVAL - 1 HOUR),
            '%y-%m-%d %H'
         ) HOUR
      FROM
         (
            SELECT
               @cdate := DATE_ADD(
                  DATE_FORMAT(NOW(), '%y-%m-%d %H'),
                  INTERVAL + 1 HOUR
               )
            FROM
               city  #記錄大於等於24條的任意一張表
         ) t0
      LIMIT 24

查詢前24小時有的數據spa

SELECT
      DATE_FORMAT(visit_time, '%y-%m-%d %H') HOUR
   FROM
      visit_log  #真正要查的記錄表
   WHERE
      visit_time >= (NOW() - INTERVAL 24 HOUR)

 

最後結果code

 

 

 

 參考http://www.javashuo.com/article/p-aaidkxvy-gr.htmlhtm

相關文章
相關標籤/搜索