Mysql-按月日期显示行组

时间:2018-12-05 19:25:55

标签: mysql

我有一个带帖子的表,我想按月和年排序,并显示所有行数据。例如,如果一个月有10行,则所有行都应该可见。

对于帖子计数,我目前使用以下查询:

SELECT  YEAR(created_at), MONTHNAME(created_at) ,Count(id)
FROM `posts` 
GROUP BY YEAR(created_at),MONTH(created_at);

这将导致:

2013    November    4
2013    December    5
2014    January     2
2014    February    1
2014    March   1
2014    November    15

但是,我希望将以下内容作为输出:

2013 November Some Post
2013 November Some Other Post
2013 December Some Other Post 2
2014 January First Post
2014 January Second Post

1 个答案:

答案 0 :(得分:1)

您实际上并不想使用GROUP BY

相反,您想使用ORDER BY

SELECT  YEAR(created_at), MONTHNAME(created_at), name
FROM `posts` 
ORDER BY YEAR(created_at), MONTH(created_at);

我假设您的帖子名称在name列中。