Count totals by year and month

只谈情不闲聊 提交于 2019-12-18 11:09:56

问题


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

回答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')



回答2:


SELECT YEAR(created) as year_val, MONTH(created) as month_val ,COUNT(*) as total
FROM testing
GROUP BY YEAR(created), MONTH(created)


来源:https://stackoverflow.com/questions/5166344/count-totals-by-year-and-month

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!