简体   繁体   English

MySQL-如何将LIMIT应用于GROUP?

[英]MySQL - How to apply LIMIT to GROUP?

I create a kind of a blog engine, where posts are categorized into month-year categories, based on timestamp. 我创建了一种博客引擎,该博客引擎根据时间戳将帖子分为月-年类别。 I use the following query: 我使用以下查询:

SELECT MONTHNAME( post_time ) AS
month , YEAR( post_time ) AS year
FROM blog_posts
GROUP BY year,
month
ORDER BY post_time DESC
LIMIT 0 , 10

which returns me the latest ten post categories like this: 返回我最新的十个帖子类别,如下所示:

December 2010
November 2010
...
etc

My question is how do I create a query to fetch older/newer categories? 我的问题是如何创建查询以获取旧/新类别?

If I had a column like *category_id* I'd be able to easily use it with LIMIT, but as month and year is all I have I'm stuck. 如果我有* category_id *这样的列,则可以轻松地使用LIMIT来使用它,但是由于我只剩下月份和年份。

Help greatly appreciated, this one's a riddle for me. 非常感谢帮助,这对我来说是个谜。

My question is how do I create a query to fetch older/newer categories? 我的问题是如何创建查询以获取旧/新类别?

This query: 该查询:

SELECT  MONTHNAME(post_time) AS month, YEAR(post_time) AS year
FROM    blog_posts
GROUP BY
        year, month
ORDER BY
        post_time DESC
LIMIT 0, 10

groups by months and years and orders by time of the random post within each month and year. 按月和年分组,按月和年内的随机发布时间排序。

Since the order of these random posts correspond to that of months and years, your categories will go out in correct order (recent to early). 由于这些随机发帖的顺序与几个月和几年的顺序相对应,因此您的类别将以正确的顺序显示(从近到早)。

Update: 更新:

To show 10 categories before June 2010 : June 2010之前显示10类别:

SELECT  MONTHNAME(post_time) AS month, YEAR(post_time) AS year
FROM    blog_posts
WHERE   post_time < '2010-06-01'
GROUP BY
        year, month
ORDER BY
        post_time DESC
LIMIT 0, 10

If blog_posts have category_id 如果blog_posts具有category_id

SELECT MONTHNAME( post_time ) AS
month , YEAR( post_time ) AS year
FROM blog_posts
GROUP BY year,
month
ORDER BY category_id DESC
LIMIT 0 , 10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM