mysql 如何统计一个字段不同值条数?

以时间为跨度 统计不同的值,在该时间出现的次数
图片描述,在这里,我要查询出1年内每个月份periods字段不同值的次数。
比如下图中可见的2015-4月,periods为2出现了3次,3出现了1次,最关键的是 periods你不知道有多少种可能的值,也许这个月有1,也许没有。

阅读 6.2k
4 个回答

用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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题