GROUP BY 得到前两行

GROUP BY and get the top two row

所以我有以下查询,它按照我的预期执行。它从每个作者那里获取最新发表的文章。但是现在我想让它从每个作者那里得到最新的两篇文章。我该怎么做?

SELECT author_article.ID
FROM (
       SELECT
         sorted_articles.ID,
         sorted_articles.AuthorID,
         sorted_articles.PublishedDate
       FROM ArticlePage sorted_articles
       ORDER BY PublishedDate DESC
     ) author_article
GROUP BY author_article.AuthorID
ORDER BY author_article.PublishedDate DESC;

所以我需要的是每个作者最新的2篇文章。

使用相关子查询来计算同一作者最近发表的所有文章。如果有 1 篇或更少的最新文章,return 行。

SELECT *
FROM ArticlePage t1
WHERE (select count(*) from ArticlePage t2
       where t2.AuthorID = t1.AuthorID
         and t2.PublishedDate > t1.PublishedDate) <= 1

如果您想要作者和文章 ID,那么这会将它们放在一行中:

SELECT ap.AuthorId,
       SUBSTRING_INDEX(GROUP_CONCAT(ap.AuthorId ORDER BY ap.PublishedDate DESC
                                   ), ',', 2) as Top2Articles
FROM ArticlePage ap
GROUP BY ap.AuthorId;

注意:group concat 中间值的默认长度是有限的,但如果某些作者有很多很多文章,则可以更改。

此外,您的原始查询正在使用 MySQL 的(错误)功能,该功能已明确记录为无法按您的预期工作。 SELECT 中有不在 GROUP BY 中的列。这些值来自 indeterminate 行,因此子查询中的 ORDER BY 可能不会按您预期的方式影响结果。

给出一个 row_number 的另一个视角。

查询

select t2.articleId,
t2.articleName,
t2.authorName,
t2.publishedDate
from
(
   select articleId,
   articleName,
   authorName,
    publishedDate,
    ( 
        case authorName 
        when @curA
        then @curRow := @curRow + 1 
       else @curRow := 1 and @curA := authorName end
    ) + 1 as rn
    from article t,
    (select @curRow := 0, @curA := '') r
    order by authorName,publishedDate desc
)t2
where t2.rn<3;

Fiddle demo