以時間為跨度 統計不同的值,在該時間出現的次數
,在這裡,我要查詢出1年內每個月份periods欄位不同值的次數。
比如中可見的2015-4月,periods為2出現了3次,3出現了1次,最關鍵的是 periods你不知道有多少種可能的值,也許這個月有1,也許沒有。
回複內容:
以時間為跨度 統計不同的值,在該時間出現的次數
,在這裡,我要查詢出1年內每個月份periods欄位不同值的次數。
比如中可見的2015-4月,periods為2出現了3次,3出現了1次,最關鍵的是 periods你不知道有多少種可能的值,也許這個月有1,也許沒有。
寫了個DEMO,線上預覽
先說下我用的時間格式是date,年-月-日
這種形式
統計次數,count再group by 列名
SELECT `periods`,DATE_FORMAT(time,'%Y-%m') as month,COUNT(*) AS `times` FROM `product` GROUP BY `periods`,`month` ORDER BY `month` DESC
DOC:
date_format:http://www.w3school.com.cn/sql/func_date_format.asp
group by:http://www.w3school.com.cn/sql/sql_groupby.asp
用count函數
用兩個group by 條件就可以了呀
select count(id), periods, from_unixtime(time, '%Y-%m')from productwhere 你的條件group by periods,from_unixtime(time, '%Y-%m')
呃,大概思路是這個樣子的
select periods,sum(periods) as count from product where month>='時間' and mouth<="時間" group by periods