获取内容组 SQL 的最新版本

发布于 2024-11-07 03:36:20 字数 381 浏览 4 评论 0原文

我有内容表,其中有许多内容和这些内容的各种版本。 这些列是 content varchardocument_id int(标识多个版本之间的内容)和 version int

我想返回每个内容组的最新版本。我尝试使用此 SQL,但它只返回第一个版本,但我想要最后一个版本。 order by 不适用于 group by。

SELECT * FROM content where id_project = 8 group by document_id order by version desc;

如何获取所有唯一内容的最新版本(唯一内容由 document_id 标识)?

谢谢如果有人可以帮助我

I have content table where I have many contents and various versions of those contents.
The columns are content varchar,document_id int (identifies a content between many versions) and version int

I want to return the lastest version of each content group. I tried using this SQL but it returns me just the first version but I want the last.The order by doesn't work with group by.

SELECT * FROM content where id_project = 8 group by document_id order by version desc;

How can i get the lastest version of all unique content(unique content is identified by the document_id)?

Thanks if anyone can help me

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

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

发布评论

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

评论(3

静谧 2024-11-14 03:36:20
SELECT c1.* FROM content c1
  left join content c2 on c1.document_id = c2.document_id and c1.version < c2.version
 where c2.document_id is null
   and c1.id_project = 8 
SELECT c1.* FROM content c1
  left join content c2 on c1.document_id = c2.document_id and c1.version < c2.version
 where c2.document_id is null
   and c1.id_project = 8 
与风相奔跑 2024-11-14 03:36:20

从内容中选择 document_id,MAX(version) WHERE id_project = 8 GROUP BY document_id

SELECT document_id,MAX(version) FROM content WHERE id_project = 8 GROUP BY document_id

盗心人 2024-11-14 03:36:20

我相信您想要类似

SELECT *
FROM content A
WHERE version = (
    SELECT MAX(version)
    FROM content B
    WHERE B.document_id = A.document_id
)
AND document_id = 8

假设每组版本均按 document_id“分组”的内容。

I believe you want something like

SELECT *
FROM content A
WHERE version = (
    SELECT MAX(version)
    FROM content B
    WHERE B.document_id = A.document_id
)
AND document_id = 8

Assuming each set of versions is "grouped" by a document_id.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文