基于最新版本的不同记录

发布于 2025-01-07 23:15:19 字数 1434 浏览 0 评论 0原文

我列出了条目的评论,并允许用户编辑现有评论,并且它会跟踪这些修订。

表结构:

comments:id,摘要

修订:comment_id,revision_id,时间戳

revisions.comment_id = comments.id

revisions.revision_id =新评论的id,进一步说明:当选择现有评论时,会显示一个编辑表单并他们可以输入新评论并提交。它将作为新评论插入到注释表中,从该表中获取最后一个 id,并将其设置为修订表中的 revision_id。

我想在修订表上执行不同的选择(以将多个 comment_id、revision_id 检索到要在应用程序中使用的数组中):

例如:

select distinct comment_id from revisions

但是是否可以根据最新的评论(revisions.txt)选择适当的记录?时间戳)?

理论上:

从修订中选择不同的 comment_id,其中时间戳是最大的

修订表示例:

comment_id   revision_id   timestamp
         2            12   20120222180000
         2            13   20120222170000
         5            18   20120222190000
         5            19   20120222200000

在这个 4 行示例中,我希望查询返回两行,

即:

comment_id = 2,revision_id = 12 timestamp = 20120222180000

comment_id = 5,revision_id = 19时间戳= 20120222200000

更新:这似乎做到这一点,但请让我知道是否有更好的方法

SELECT 
 distinct comment_id, max(timestamp)
FROM
 revisions 
GROUP BY
 comment_id

更新:我还需要包含 revision_id,上面的查询仅包含 comment_id 和时间戳

这做到了:

SELECT 
 distinct a.comment_id as comment_id, 
 a.revision_id revision_id,
 a.timestamp as timestamp
FROM
 REVISIONS a 
WHERE
 a.timestamp = ( 
 SELECT
  max(b.timestamp)
 FROM
  revisions b
 WHERE
  b.comment_id = a.comment_id
)

I list comments for an entry and I allow for user's to edit existing comments and it keeps track of those revisions.

Table structure:

comments: id, summary

revisions: comment_id, revision_id, timestamp

revisions.comment_id = comments.id

revisions.revision_id = the id of the new comment, further explanation: when they select the existing comment, it will display an edit form and they may enter a new comment and submit it. It will insert it into the comments table as a new comment, grab the last id from that table and set it as revision_id in the revisions table.

I want to do a select distinct on the revisions table (to retrieve multiple comment_id, revision_id into an array to be used within the application):

for example:

select distinct comment_id from revisions

But is it possible to select the appropriate record based on the most recent comment (revisions.timestamp)?

in theory:

select distinct comment_id from revisions WHERE timestamp IS THE LARGEST

revisions table example:

comment_id   revision_id   timestamp
         2            12   20120222180000
         2            13   20120222170000
         5            18   20120222190000
         5            19   20120222200000

In this example of 4 rows, I'd want the query to return two rows,

that being:

comment_id = 2, revision_id = 12 timestamp = 20120222180000

and

comment_id = 5, revision_id = 19 timestamp = 20120222200000

UPDATE: this seems to do the trick, but please let me know if there is a better way

SELECT 
 distinct comment_id, max(timestamp)
FROM
 revisions 
GROUP BY
 comment_id

UPDATE: I also need to include revision_id, the above query only includes comment_id and timestamp

This did it:

SELECT 
 distinct a.comment_id as comment_id, 
 a.revision_id revision_id,
 a.timestamp as timestamp
FROM
 REVISIONS a 
WHERE
 a.timestamp = ( 
 SELECT
  max(b.timestamp)
 FROM
  revisions b
 WHERE
  b.comment_id = a.comment_id
)

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

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

发布评论

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

评论(1

橘虞初梦 2025-01-14 23:15:19

如果我理解正确,您只需按时间戳降序排序并获取第一个结果。

SELECT comment_id FROM revisions ORDER BY timestamp DESC LIMIT 0,1

编辑:我想我现在明白了,你真正想要什么,试试这个:

SELECT * FROM revisions GROUP BY comment_id ORDER BY timestamp DESC  

If I understand you correctly, you just sort descending by timestamp and take the first result.

SELECT comment_id FROM revisions ORDER BY timestamp DESC LIMIT 0,1

Edit: I think I now understand, what you really want, try this:

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