扫码关注官方订阅号
以时间为跨度 统计不同的值,在该时间出现的次数,在这里,我要查询出1年内每个月份periods字段不同值的次数。比如下图中可见的2015-4月,periods为2出现了3次,3出现了1次,最关键的是 periods你不知道有多少种可能的值,也许这个月有1,也许没有。
人生最曼妙的风景,竟是内心的淡定与从容!
写了个DEMO,在线预览
先说下我用的时间格式是date,年-月-日这种形式
年-月-日
统计次数,count再group by 列名
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.aspgroup by:http://www.w3school.com.cn/sql/sql_groupby.asp
用count函数
用两个group by 条件就可以了呀
select count(id), periods, from_unixtime(time, '%Y-%m') from product where 你的条件 group by periods,from_unixtime(time, '%Y-%m')
呃,大概思路是这个样子的
select periods,sum(periods) as count from product where month>='时间' and mouth<="时间" group by periods
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
写了个DEMO,在线预览
先说下我用的时间格式是date,
年-月-日这种形式统计次数,
count再group by 列名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 periods,sum(periods) as count from product where month>='时间' and mouth<="时间" group by periods