補齊上篇查詢一年中每月數據,某月份數據爲空補0問題(MySql)

1、查詢一年中每月數據,某月份數據爲空補0

 SELECT
sum(case DATE_FORMAT(日期字段,'%m') when '01' then 1 else 0 end) as 一月,
sum(case DATE_FORMAT(日期字段,'%m') when '02' then 1 else 0 end) as 二月,
sum(case DATE_FORMAT(日期字段,'%m') when '03' then 1 else 0 end) as 三月,
sum(case DATE_FORMAT(日期字段,'%m') when '04' then 1 else 0 end) as 四月,
sum(case DATE_FORMAT(日期字段,'%m') when '05' then 1 else 0 end) as 五月,
sum(case DATE_FORMAT(日期字段,'%m') when '06' then 1 else 0 end) as 六月,
sum(case DATE_FORMAT(日期字段,'%m') when '07' then 1 else 0 end) as 七月,
sum(case DATE_FORMAT(日期字段,'%m') when '08' then 1 else 0 end) as 八月,
sum(case DATE_FORMAT(日期字段,'%m') when '09' then 1 else 0 end) as 九月,
sum(case DATE_FORMAT(日期字段,'%m') when '10' then 1 else 0 end) as 十月,
sum(case DATE_FORMAT(日期字段,'%m') when '11' then 1 else 0 end) as 十一月,
sum(case DATE_FORMAT(日期字段,'%m') when '12' then 1 else 0 end) as 十二月
FROM 表名
WHERE  日期字段like ?(備註:?佔位符代表在後端代碼中用Calendar對象獲取年份字符串並傳參進入)

2、查詢某月每天數據統計,沒有數據補0(同上)

 SELECT
sum(case DATE_FORMAT(日期字段,'%d') when '01' then 1 else 0 end) as 1日,
sum(case DATE_FORMAT(日期字段,'%d') when '02' then 1 else 0 end) as 2日,
sum(case DATE_FORMAT(日期字段,'%d') when '03' then 1 else 0 end) as 3日,
sum(case DATE_FORMAT(日期字段,'%d') when '04' then 1 else 0 end) as 4日,
sum(case DATE_FORMAT(日期字段,'%d') when '05' then 1 else 0 end) as 5日,
sum(case DATE_FORMAT(日期字段,'%d') when '06' then 1 else 0 end) as 6日,
sum(case DATE_FORMAT(日期字段,'%d') when '07' then 1 else 0 end) as 7日,
sum(case DATE_FORMAT(日期字段,'%d') when '08' then 1 else 0 end) as 8日,
sum(case DATE_FORMAT(日期字段,'%d') when '09' then 1 else 0 end) as 9日,
sum(case DATE_FORMAT(日期字段,'%d') when '10' then 1 else 0 end) as 10日,
sum(case DATE_FORMAT(日期字段,'%d') when '11' then 1 else 0 end) as 11日,
sum(case DATE_FORMAT(日期字段,'%d') when '12' then 1 else 0 end) as 12日,
sum(case DATE_FORMAT(日期字段,'%d') when '13' then 1 else 0 end) as 13日,
sum(case DATE_FORMAT(日期字段,'%d') when '14' then 1 else 0 end) as 14日,
sum(case DATE_FORMAT(日期字段,'%d') when '15' then 1 else 0 end) as 15日,
sum(case DATE_FORMAT(日期字段,'%d') when '16' then 1 else 0 end) as 16日,
sum(case DATE_FORMAT(日期字段,'%d') when '17' then 1 else 0 end) as 17日,
sum(case DATE_FORMAT(日期字段,'%d') when '18' then 1 else 0 end) as 18日,
sum(case DATE_FORMAT(日期字段,'%d') when '19' then 1 else 0 end) as 19日,
sum(case DATE_FORMAT(日期字段,'%d') when '20' then 1 else 0 end) as 20日,
sum(case DATE_FORMAT(日期字段,'%d') when '21' then 1 else 0 end) as 21日,
sum(case DATE_FORMAT(日期字段,'%d') when '22' then 1 else 0 end) as 22日,
sum(case DATE_FORMAT(日期字段,'%d') when '23' then 1 else 0 end) as 23日,
sum(case DATE_FORMAT(日期字段,'%d') when '24' then 1 else 0 end) as 24日,
sum(case DATE_FORMAT(日期字段,'%d') when '25' then 1 else 0 end) as 25日,
sum(case DATE_FORMAT(日期字段,'%d') when '26' then 1 else 0 end) as 26日,
sum(case DATE_FORMAT(日期字段,'%d') when '27' then 1 else 0 end) as 27日,
sum(case DATE_FORMAT(日期字段,'%d') when '28' then 1 else 0 end) as 28日,
sum(case DATE_FORMAT(日期字段,'%d') when '29' then 1 else 0 end) as 29日,
sum(case DATE_FORMAT(日期字段,'%d') when '30' then 1 else 0 end) as 30日,
sum(case DATE_FORMAT(日期字段,'%d') when '31' then 1 else 0 end) as 31日
FROM 表名
WHERE MONTH(日期字段) = MONTH( NOW( ) )(備註:在sql語句中獲取當前月份)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章