mysql 按年度、季度、月度、周、日SQL統計查詢

 表結構:

 表中數據總條數:2926

1. 統計每天的單數

  SELECT COUNT(*),DATE_FORMAT(t.create_time,'%Y-%m-%d') from t_call_record t GROUP BY DATE_FORMAT(t.create_time,'%Y-%m-%d')  (執行時間:0.003s)

   SELECT count(*),DATE_FORMAT(t.create_time,'%Y-%m-%d') from t_call_record t GROUP BY TO_DAYS(t.create_time)  (執行時間:0.002s)

第二種優於第一種,會列出所有的單子

1.2 統計上個月的天數據

SELECT count(*),DATE_FORMAT(t.create_time,'%Y-%m-%d') from t_call_record t where YEAR(t.create_time) = '2018' GROUP BY DAY(t.create_time)
(執行時間:0.001s)按天統計從上個月最後一天向前推的31天數據

1.3 按周統計數據

SELECT count(*),WEEK(t.create_time),MONTH(t.create_time) from t_call_record t where YEAR(t.create_time) = '2018' GROUP BY WEEK(t.create_time)(執行時間:0.002s)
1.4 按月統計數據

SELECT count(*),MONTH(t.create_time) from t_call_record t where YEAR(t.create_time) = '2018' GROUP BY MONTH(t.create_time)
(執行時間:0.002s)

1.5 按照年統計數據

SELECT count(*),YEAR(t.create_time) from t_call_record t where YEAR(t.create_time) = '2018' GROUP BY YEAR(t.create_time)(執行時間:0.001s)
1.6 按照季度統計數據

SELECT count(*),QUARTER(t.create_time) from t_call_record t where YEAR(t.create_time) = '2018' GROUP BY QUARTER(t.create_time)
(執行時間:0.001s)

1.7 查詢本年度數據

SELECT * from t_call_record t WHERE year(t.create_time) = year(curdate()) (執行時間:0.005s)

1.8 查詢本季度數據

SELECT *  from t_call_record where quater(t.create_time) = QUARTER(curdate()) (執行時間0.004s)

1.9 查詢本月度數據

SELECT * from t_call_record  where MONTH(create_time) = MONTH(CURDATE()) and year(create_time) = year(curdate())(執行時間:0.004s)

1.20 查詢本週數據

SELECT * from t_call_record t where MONTH(t.create_time) = MONTH(CURDATE()) and WEEK(t.create_time) = WEEK(CURDATE())(執行時間0.003s)

1.21 查詢近5天的記錄

SELECT * from t_call_record t where TO_DAYS(NOW())-TO_DAYS(t.create_time) <= 5(執行時間: 0.003s)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章