查詢每月數據統計

select m.id month, IFNULL(b,0) num from
(
  select 1 as id union select 2 union select 3 union select 4 union select 5 union select 6
  union select 7 union select 8 union select 9 union select 10 union select 11 union select 12
) m
left join 
(
	SELECT
		MONTH (create_time) a,
		count(user_account_id) b
	FROM
		tb_activity_reg
	WHERE
		YEAR (create_time) = YEAR (NOW())
	GROUP BY
		MONTH (create_time)
) n
on m.id = n.a  
ORDER BY month asc 

 

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