SQL Server:根据多对n:m关系中的最新id进行选择

发布于 2024-10-21 14:13:52 字数 607 浏览 1 评论 0原文

我需要做的是选择评论详细信息以及对评论采取的最后操作;我有 3 个表:

Comment

CommentID, commentText, userID, date_posted

Action

ActionID, action_taken,userID,date_actioned

和 CommentJoinAction

id,ActionID,CommentID

可以有一个评论,但该评论可以有多个操作。

我的 SQL 看起来像这样:

Select /*snip comment details and such*/
From Comment
Inner Join (select max(actionid) from commentjoinaction) as cja on /*blah cause you know from reading this, it won't work*/

那么我可以做什么,以便我始终为评论获取最新的 commentAction 。

非常感谢

What i need to do is select the comment details and the last action taken on the comment; I have 3 tables:

Comment

CommentID, commentText, userID, date_posted

Action

ActionID, action_taken,userID,date_actioned

and CommentJoinAction

id,ActionID,CommentID

There can be one comment, but many actions on the comment.

my SQL is looking something like:

Select /*snip comment details and such*/
From Comment
Inner Join (select max(actionid) from commentjoinaction) as cja on /*blah cause you know from reading this, it won't work*/

So what is it that I can do so that I always pick up the latest commentAction for the comments.

many thanks

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

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

发布评论

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

评论(4

乖乖兔^ω^ 2024-10-28 14:13:53

这是您要找的吗?

SELECT 
/*Select desired fields*/
FROM Comments AS C
    INNER JOIN (
                SELECT 
                    CommentID
                    ,MAX(ActionID) AS ActionID
                FROM CommentJoinAction
                GROUP BY CommentID
            )AS CJA
        ON C.CommentID = CJA.CommentID
        INNER JOIN ACTION AS A
            ON CJA.ActionID = A.ActionID

Is this what you are looking for?

SELECT 
/*Select desired fields*/
FROM Comments AS C
    INNER JOIN (
                SELECT 
                    CommentID
                    ,MAX(ActionID) AS ActionID
                FROM CommentJoinAction
                GROUP BY CommentID
            )AS CJA
        ON C.CommentID = CJA.CommentID
        INNER JOIN ACTION AS A
            ON CJA.ActionID = A.ActionID
慕烟庭风 2024-10-28 14:13:53
select C.*, A.* from Comment C
inner join 
(
    select CommentID, Max(ActionID) as LatestActionID from CommentJoinAction
    group by CommentID
) CJA on C.CommentID = CJA.CommentID
inner join Action A on CJA.LatestActionID = A.ActionID
select C.*, A.* from Comment C
inner join 
(
    select CommentID, Max(ActionID) as LatestActionID from CommentJoinAction
    group by CommentID
) CJA on C.CommentID = CJA.CommentID
inner join Action A on CJA.LatestActionID = A.ActionID
你好,陌生人 2024-10-28 14:13:53

如果您只想要 actionID

select c.*, (
  select max(actionID) 
  from CommentJoinAction cja 
  where cja.commentID = c.commentID
) as maxActionID
from Comment c

或者如果您想要所有 Action 字段:

select c.*, a.*
from Comment c 
inner join Action a 
  on a.actionID =     (
   select max(actionID) 
   from CommentJoinAction 
   where commentID = c.commentID
)

If you just want the actionID

select c.*, (
  select max(actionID) 
  from CommentJoinAction cja 
  where cja.commentID = c.commentID
) as maxActionID
from Comment c

Or if you want all the Action fields:

select c.*, a.*
from Comment c 
inner join Action a 
  on a.actionID =     (
   select max(actionID) 
   from CommentJoinAction 
   where commentID = c.commentID
)
若水微香 2024-10-28 14:13:52
SELECT t.commentText, t.action_taken
    FROM (SELECT c.commentText, a.action_taken,
                 ROW_NUMBER() OVER (PARTITION BY c.CommentID ORDER BY a.date_actioned DESC) AS RowNum
              FROM Comment c
                  INNER JOIN CommentJoinAction cja
                      ON c.CommentID = cja.CommentID
                  INNER JOIN Action a
                      ON cja.ActionID = a.ActionID
          ) t
    WHERE t.RowNum = 1
SELECT t.commentText, t.action_taken
    FROM (SELECT c.commentText, a.action_taken,
                 ROW_NUMBER() OVER (PARTITION BY c.CommentID ORDER BY a.date_actioned DESC) AS RowNum
              FROM Comment c
                  INNER JOIN CommentJoinAction cja
                      ON c.CommentID = cja.CommentID
                  INNER JOIN Action a
                      ON cja.ActionID = a.ActionID
          ) t
    WHERE t.RowNum = 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文