为每个类别选择 N 条记录并按 X 排序

发布于 2024-09-25 17:48:45 字数 182 浏览 2 评论 0原文

我有一个包含博客文章的数据库表。我想在主页上显示每个类别的一篇(或多篇)帖子,例如按日期排序。

所以我的帖子表如下所示:id |标题 |描述 |猫 |文件名 | date

我将如何创建这样的查询?我想过使用 group-by 或子选择,但我不确定这对性能是否有好处......该表有大量记录。

I have a database table that contains blog posts. I want to show on the homepage one (or more) post for each category, ordering by date, for example.

So my posts table looks like this: id | title | description | cat | filename | date

How would I create such a query? I've thought to use group-by or a subselect but I'm not sure if it's a good thing for performance... the table has a large number of records.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

煮酒 2024-10-02 17:48:45

MySQL 支持分析函数(ROW_NUMBER、RANK、DENSE_RANK、NTILE...),但您可以使用变量来模拟该功能。

如果您想要 N 个最新博客文章:

SELECT x.id,
       x.title,
       x.description,
       x.cat,
       x.filename,
       x.date
  FROM (SELECT bp.id,
               bp.title,
               bp.description,
               bp.cat,
               bp.filename,
               bp.date,
               CASE 
                 WHEN bp.cat = @category THEN @rownum := @rownum + 1
                 ELSE @rownum := 1
               END AS rank,
               @category := bp.cat
          FROM BLOG_POSTS bp
          JOIN (SELECT @rownum := 0, @category := NULL) r
      ORDER BY bp.cat, bp.date DESC) x
 WHERE x.rank <= N

如果您希望排名 1 的博客文章成为最早的博客文章,请将 ORDER BY 更改为:

ORDER BY bp.cat, bp.date

MySQL doesn't support analytic functions (ROW_NUMBER, RANK, DENSE_RANK, NTILE...), but you can emulate the functionality with variables.

If you want the N most recent blog posts:

SELECT x.id,
       x.title,
       x.description,
       x.cat,
       x.filename,
       x.date
  FROM (SELECT bp.id,
               bp.title,
               bp.description,
               bp.cat,
               bp.filename,
               bp.date,
               CASE 
                 WHEN bp.cat = @category THEN @rownum := @rownum + 1
                 ELSE @rownum := 1
               END AS rank,
               @category := bp.cat
          FROM BLOG_POSTS bp
          JOIN (SELECT @rownum := 0, @category := NULL) r
      ORDER BY bp.cat, bp.date DESC) x
 WHERE x.rank <= N

If you want rank of 1 to be the earliest blog post, change the ORDER BY to:

ORDER BY bp.cat, bp.date
[浮城] 2024-10-02 17:48:45

使用更现代的 SQL,使用 CTE 和 Windows 函数(在 PostgreSQL 9.3 中测试,但我怀疑它也适用于最新版本的 MySQL),以显示每个类别 2 个标题:

WITH b AS
  (SELECT title, cat, row_number() OVER (PARTITION BY cat) as rn FROM BLOGS)
SELECT title, cat FROM b WHERE rn <= 2;

With more modern SQL, using CTE and Windows Functions (tested in PostgreSQL 9.3, but I suspect it'll work on a recent version of MySQL too), to show say 2 title per category:

WITH b AS
  (SELECT title, cat, row_number() OVER (PARTITION BY cat) as rn FROM BLOGS)
SELECT title, cat FROM b WHERE rn <= 2;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文