SQL 连接到最佳匹配行

发布于 2024-09-06 06:35:27 字数 206 浏览 4 评论 0原文

我有一个维基系统,其中有一个中心表“文章”,该表在自己的表中包含许多修订。每个修订都包含一个created_at 时间和日期列。

我想更新文章以包含最新修订名称字段中的非规范化字段 sort_name 。

我可以发出什么 SQL 命令来用其最新修订的名称字段填充每篇文章的 sort_name 字段?

就其价值而言,我使用的是 PostgreSQL。

I have a wiki system where there is a central table, Article, that has many Revisions in their own table. The Revisions each contain a created_at time and date column.

I want to update the Articles to contain a denormalized field sort_name from the most recent Revision's name field.

What SQL command can I issue to fill in each Article's sort_name field with its most recent Revision's name field?

For what it's worth, I'm on PostgreSQL.

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

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

发布评论

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

评论(3

江心雾 2024-09-13 06:35:27

不是 100% 确定 PostgreSQL 语法,所以你可能需要稍微改变一下,但想法是:

UPDATE Articles
SET sort_name = (SELECT FIRST(name)
                 FROM Revisions
                 WHERE Revisions.id = Articles.id
                 ORDER BY created_at DESC)

Not 100% sure the PostgreSQL syntax, so you might have to change this a little, but the idea is:

UPDATE Articles
SET sort_name = (SELECT FIRST(name)
                 FROM Revisions
                 WHERE Revisions.id = Articles.id
                 ORDER BY created_at DESC)
少女七分熟 2024-09-13 06:35:27

表结构将有助于更好地定义您的问题。

对于 Postgres:

UPDATE ARTICLES ar
SET sort_name = (SELECT name FROM Revisions rev 
                 WHERE rev.article_id = ar.article_id
                 ORDER BY rev.createdate DESC LIMIT 1)

Table structure would help define your problem better.

For Postgres:

UPDATE ARTICLES ar
SET sort_name = (SELECT name FROM Revisions rev 
                 WHERE rev.article_id = ar.article_id
                 ORDER BY rev.createdate DESC LIMIT 1)
童话里做英雄 2024-09-13 06:35:27

如果我理解正确的话,您想加入最新的修订版。这是选择最新版本进行更新的优化方法。如果这不是您要找的,请告诉我。它是为 T-SQL 编写的,因为我不熟悉 PostgreSQL

UPDATE ARTICLES
SET SORT_NAME = lastHist.NAME
FROM ARTICLES AS t
    --Join the the most recent history entries
        INNER JOIN  REVISION lastHis ON t.ID = lastHis.FK_ID
        --limits to the last history in the WHERE statement
            LEFT JOIN REVISION his2 on lastHis.FK_ID = his2.FK_ID and lastHis.CREATED_TIME < his2.CREATED_TIME
WHERE his2.ID is null

If I understand you correctly, you want to join to the most recent revision. Here is an optimized way to select the most recent revision to do the update. Let me know if this wasn't what you were looking for. It's written for T-SQL since I'm not familiar with PostgreSQL

UPDATE ARTICLES
SET SORT_NAME = lastHist.NAME
FROM ARTICLES AS t
    --Join the the most recent history entries
        INNER JOIN  REVISION lastHis ON t.ID = lastHis.FK_ID
        --limits to the last history in the WHERE statement
            LEFT JOIN REVISION his2 on lastHis.FK_ID = his2.FK_ID and lastHis.CREATED_TIME < his2.CREATED_TIME
WHERE his2.ID is null
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文