按年份和月份计算总计 [英] Count totals by year and month

查看:155
本文介绍了按年份和月份计算总计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  id,created,action 
1,'2011- 01-01 04:28:21','注册'
2,'2011-01-05 04:28:21','注册'
3',2011-02-02 04:28 :21','注册'

如何选择和分组这些输出如下:

 年,月,总
2011,1,2
2011,2,1


解决方案

试试这个:

  SELECT DATE_FORMAT(创建'%Y')为'year',
DATE_FORMAT(创建'%m')为'month',
COUNT id)作为'total'
FROM table_name
GROUP BY DATE_FORMAT(创建,'%Y%m')


I have a table that looks like this:

id,created,action
1,'2011-01-01 04:28:21','signup'
2,'2011-01-05 04:28:21','signup'
3,'2011-02-02 04:28:21','signup'

How do I select and group these so the output is:

year,month,total
2011,1,2
2011,2,1

解决方案

Try this:

SELECT DATE_FORMAT(created, '%Y') as 'year',
DATE_FORMAT(created, '%m') as 'month',
COUNT(id) as 'total'
FROM table_name
GROUP BY DATE_FORMAT(created, '%Y%m')

这篇关于按年份和月份计算总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆