我正在尝试进行查询以从数据库中获取数据,但我无法完成它。
我的下表有虚拟数据:
id | date
0 | 2011-11-25 20:12:32
1 | 2011-11-15 20:12:32
2 | 2011-11-05 20:12:32
3 | 2011-10-25 20:12:32
4 | 2011-10-15 20:12:32
5 | 2011-10-05 20:12:32
6 | 2010-10-25 20:12:32
7 | 2010-04-25 20:12:32
8 | 2009-07-25 20:12:32
我想提出一个查询:
如果你没理解我的意思: 它应该给出这样的结果(使用表中的虚拟数据):
Year | Month | amount (of rows with that month in that year)
2011 | 11 | 3
2011 | 10 | 3
2010 | 4 | 2
2009 | 7 | 1
我对mySQL有一些了解,但这对我来说很重要。 :)
提前致谢
答案 0 :(得分:10)
SELECT YEAR(`date`) AS `year`,
MONTH(`date`) AS `month`,
COUNT(*) AS amount
FROM `table`
GROUP BY YEAR(`date`),
MONTH(`date`)
ORDER BY `date` DESC
答案 1 :(得分:1)
SELECT YEAR(date) AS Year, MONTH(date) AS Month, COUNT(*) as amount
FROM table
GROUP BY LEFT(date, 7)
ORDER BY date DESC
答案 2 :(得分:0)
在您的查询中使用此项:
SELECT count(*) from t GROUP BY YEAR(date), MONTH(date)