根据最新评论日期显示父行后面的子行

时间:2017-01-04 06:29:24

标签: sql-server-2008

我创建并显示博客评论数据,我必须按顺序制作数据。 下面是使用表的表结构和插入查询:

CREATE TABLE [dbo].[Blog_CommentDetails](
    [CommentID] [int] IDENTITY(1,1) NOT NULL,
    [CommentUserName] [nvarchar](200) NOT NULL,
    [CommentText] [nvarchar](max) NULL,
    [CommentApprovedByUserID] [int] NULL,
    [CommentPostDocumentID] [int] NOT NULL,
    [CommentDate] [datetime] NULL DEFAULT (getdate()),
    [HtmlComment] [nvarchar](max) NULL,
    [CommentIsSpam] [bit] NULL CONSTRAINT [DEFAULT_Blog_MainComment_CommentIsSpam]  DEFAULT ((0)),
    [CommentIsApproved] [bit] NULL CONSTRAINT [DEFAULT_Blog_MainComment_CommentIsApproved]  DEFAULT ((0)),
    [CommentEmail] [nvarchar](250) NULL,
    [CommentInfo] [nvarchar](max) NULL,
    [ReplyCommentID] [int] NULL,
    [IsRejected] [bit] NULL CONSTRAINT [DEFAULT_Blog_MainComment_CommentIsRejected]  DEFAULT ((0)),
    [PageType] [varchar](50) NULL CONSTRAINT [DEFAULT_PageType]  DEFAULT ('Blog'),
    [BlogSiteID] [int] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[Blog_CommentDetails] ON 

GO
INSERT [dbo].[Blog_CommentDetails] ([CommentID], [CommentUserName], [CommentText], [CommentApprovedByUserID], [CommentPostDocumentID], [CommentDate], [HtmlComment], [CommentIsSpam], [CommentIsApproved], [CommentEmail], [CommentInfo], [ReplyCommentID], [IsRejected], [PageType], [BlogSiteID]) VALUES (142, N'Siddappa', N'Siddappa commented on vikash comment', 70, 15, CAST(N'2016-12-30 03:52:04.533' AS DateTime), N'<div class="reply-comment-section"><div class="row"><div class="col-xs-12"><div class="post-comment-section"><p id="pBlogReplyCommentorName"><p id="sReplyCommentorName" class="blogcommentname">Siddappa</p><span id="sBlogReplyCommentDate">Dec30,2016 3:21PM</span></p><p class="comment-detail" id="pBlogReplyCommentDetails">Siddappa commented on vikash comment</p><div class="comment-reply"><ul><li id="HiddenMainreply"><img id="imgMainReply" class="contributors-list" src="/WorldVentures/getmedia/cfea575e-2f94-48f4-949f-fada2ca45e4e/icon_reply_1.png.aspx?width=128&height=128&ext=.png" alt="Reply comment icon"><a class="joinus-link" id="btnCommentReply" href="#" target="_self">Reply</a></li></ul></div></div></div></div></div>', 0, 1, N'siddappa@gmail.com', NULL, 141, 0, N'Blog', 17)
GO
INSERT [dbo].[Blog_CommentDetails] ([CommentID], [CommentUserName], [CommentText], [CommentApprovedByUserID], [CommentPostDocumentID], [CommentDate], [HtmlComment], [CommentIsSpam], [CommentIsApproved], [CommentEmail], [CommentInfo], [ReplyCommentID], [IsRejected], [PageType], [BlogSiteID]) VALUES (145, N'Suresh', N'Suresh commented on Mansur comment.', 70, 15, CAST(N'2016-12-30 03:55:50.197' AS DateTime), N'<div class="reply-comment-section"><div class="row"><div class="col-xs-12"><div class="post-comment-section"><p id="pBlogReplyCommentorName"><p id="sReplyCommentorName" class="blogcommentname">Suresh</p><span id="sBlogReplyCommentDate">Dec30,2016 3:24PM</span></p><p class="comment-detail" id="pBlogReplyCommentDetails">Suresh commented on Mansur comment.</p><div class="comment-reply"><ul><li id="HiddenMainreply"><img id="imgMainReply" class="contributors-list" src="/WorldVentures/getmedia/cfea575e-2f94-48f4-949f-fada2ca45e4e/icon_reply_1.png.aspx?width=128&height=128&ext=.png" alt="Reply comment icon"><a class="joinus-link" id="btnCommentReply" href="#" target="_self">Reply</a></li></ul></div></div></div></div></div>', 0, 1, N'suresh@gmail.com', NULL, 144, 0, N'Blog', 17)
GO
INSERT [dbo].[Blog_CommentDetails] ([CommentID], [CommentUserName], [CommentText], [CommentApprovedByUserID], [CommentPostDocumentID], [CommentDate], [HtmlComment], [CommentIsSpam], [CommentIsApproved], [CommentEmail], [CommentInfo], [ReplyCommentID], [IsRejected], [PageType], [BlogSiteID]) VALUES (141, N'Vikash', N'this is vikash main comment', 70, 15, CAST(N'2016-12-30 03:50:46.113' AS DateTime), N'<div class="main-comment-section"><div class="row"><div class="col-xs-12"><div class="post-comment-section"><p id="sCommentorName" class="blogcommentname">Vikash</p><span id="sBlogPostedDate">Dec30,2016 3:20PM</span></p><p class="comment-detail" id="pBlogCommentDetails">this is vikash main comment</p><div class="comment-reply"><ul><li id="Hiddenreply"><img id="imgReply" src="/WorldVentures/getmedia/cfea575e-2f94-48f4-949f-fada2ca45e4e/icon_reply_1.png.aspx?width=128&height=128&ext=.png" alt="Reply comment icon"><a class="joinus-link" id="btnComment" href="#" target="_self">Reply</a></li></ul></div></div></div></div></div>', 0, 1, N'vikash@gmail.com', NULL, NULL, 0, N'Blog', 17)
GO
INSERT [dbo].[Blog_CommentDetails] ([CommentID], [CommentUserName], [CommentText], [CommentApprovedByUserID], [CommentPostDocumentID], [CommentDate], [HtmlComment], [CommentIsSpam], [CommentIsApproved], [CommentEmail], [CommentInfo], [ReplyCommentID], [IsRejected], [PageType], [BlogSiteID]) VALUES (143, N'Megha', N'this is megha comment.', 70, 15, CAST(N'2016-12-30 03:53:03.633' AS DateTime), N'<div class="reply-comment-section"><div class="row"><div class="col-xs-12"><div class="post-comment-section"><p id="pBlogReplyCommentorName"><p id="sReplyCommentorName" class="blogcommentname">Megha</p><span id="sBlogReplyCommentDate">Dec30,2016 3:22PM</span></p><p class="comment-detail" id="pBlogReplyCommentDetails">this is megha comment.</p><div class="comment-reply"><ul><li id="HiddenMainreply"><img id="imgMainReply" class="contributors-list" src="/WorldVentures/getmedia/cfea575e-2f94-48f4-949f-fada2ca45e4e/icon_reply_1.png.aspx?width=128&height=128&ext=.png" alt="Reply comment icon"><a class="joinus-link" id="btnCommentReply" href="#" target="_self">Reply</a></li></ul></div></div></div></div></div>', 0, 1, N'megha@gmail.com', NULL, 141, 0, N'Blog', 17)
GO
INSERT [dbo].[Blog_CommentDetails] ([CommentID], [CommentUserName], [CommentText], [CommentApprovedByUserID], [CommentPostDocumentID], [CommentDate], [HtmlComment], [CommentIsSpam], [CommentIsApproved], [CommentEmail], [CommentInfo], [ReplyCommentID], [IsRejected], [PageType], [BlogSiteID]) VALUES (144, N'Mansur', N'mansur commented on vikash comment.', 70, 15, CAST(N'2016-12-30 03:54:12.613' AS DateTime), N'<div class="reply-comment-section"><div class="row"><div class="col-xs-12"><div class="post-comment-section"><p id="pBlogReplyCommentorName"><p id="sReplyCommentorName" class="blogcommentname">Mansur</p><span id="sBlogReplyCommentDate">Dec30,2016 3:23PM</span></p><p class="comment-detail" id="pBlogReplyCommentDetails">mansur commented on vikash comment.</p><div class="comment-reply"><ul><li id="HiddenMainreply"><img id="imgMainReply" class="contributors-list" src="/WorldVentures/getmedia/cfea575e-2f94-48f4-949f-fada2ca45e4e/icon_reply_1.png.aspx?width=128&height=128&ext=.png" alt="Reply comment icon"><a class="joinus-link" id="btnCommentReply" href="#" target="_self">Reply</a></li></ul></div></div></div></div></div>', 0, 1, N'mansur@gmail.com', NULL, 141, 0, N'Blog', 17)
GO
SET IDENTITY_INSERT [dbo].[Blog_CommentDetails] OFF
GO

我正在使用当前以下的查询来检查对其父评论回复的人:

WITH CTE AS ( SELECT CommentID ,
                    CommentUserName,
                     CommentPostDocumentID ,
                     CommentIsApproved,
                     CommentDate ,
                     ReplyCommentID ,
                     CommentID AS ThreadID ,
                     CAST( CommentID AS VARCHAR( MAX ) ) AS PathStr
              FROM Blog_CommentDetails AS T WITH(NOLOCK)
              WHERE ReplyCommentID IS NULL
              UNION ALL
              SELECT T.CommentID ,
                     T.CommentUserName,
                     t.CommentPostDocumentID ,
                     t.CommentIsApproved,
                     T.CommentDate ,
                     T.ReplyCommentID ,
                     CTE.ThreadID ,
                     PathStr + '-'+ CAST( T.ReplyCommentID AS VARCHAR( MAX ) ) AS PathStr
              FROM Blog_CommentDetails AS T WITH(NOLOCK)
              JOIN CTE 
              ON T.ReplyCommentID = CTE.CommentID
              WHERE T.ReplyCommentID IS NOT NULL )
            SELECT *
            FROM CTE
            WHERE CommentPostDocumentID = 15 AND CommentIsApproved=1
            ORDER BY ThreadID ,
                        PathStr ,
                        CommentDate DESC

使用此查询后,我可以获得如下所示的层次结构:

主要评论:Vikash    回复评论:Mansur有最新的评论,所以他就在vikash之后就座。

如果有人会再次回复vikash,他将在vikash之后根据最新插入的评论发言。

但是如果任何用户都会回复任何回复评论,就像在这个查询中一样,suresh已经回复了mansur所以他应该在mansur之后坐下来,但是他在最后一个位置。

以下附图供您参考:

Comment Image

如果您有任何疑问,请与我们联系。

0 个答案:

没有答案