- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在我的应用程序中使用博客评论,我将每个评论行插入一个表中,因此如果其中任何一个评论对单击的 commentid
上的特定评论进行回复,我将插入为 replyid
在新行中。
下面是屏幕截图:
在这里您可以看到 commentid
24 和 26 的 replycommentid
23。我需要一个查询来显示 23 之后的 24 和 26。因为 23 是 24 和 26 的父级。
以下是表格布局和示例数据的设置脚本:
USE [myDB]
GO
/****** Object: Table [dbo].[Blog_CommentDetails] Script Date: 11/12/2016 6:36:04 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
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
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
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]) VALUES (22, N'Vikash', N'This is main comment', NULL, 1, CAST(N'2016-11-12 17:36:25.637' AS DateTime), N'<div class="main-comment-section"><div class="row"><div class="col-xs-12"><div class="post-comment-section"><p id="pCommentorName"><span id="sCommentorName" class="blogcommentname">Vikash</span><span id="sBlogPostedDate">Nov12,2016 5:35PM</span></p><p class="comment-detail" id="pBlogCommentDetails">This is main comment</p><div class="comment-reply"><ul><li><img src="assets/svg/components/blog-detail/icon_reply.svg" alt="Reply comment icon"><a class="joinus-link" id="btnComment" href="#" target="_self">::blogdetailsReply</a></li></ul></div></div></div></div></div>', 0, 1, N'vikash.kr@sonata-software.com', NULL, NULL)
GO
INSERT [dbo].[Blog_CommentDetails] ([CommentID], [CommentUserName], [CommentText], [CommentApprovedByUserID], [CommentPostDocumentID], [CommentDate], [HtmlComment], [CommentIsSpam], [CommentIsApproved], [CommentEmail], [CommentInfo], [ReplyCommentID]) VALUES (23, N'Megha k', N'This is reply comment', NULL, 1, CAST(N'2016-11-12 17:39:04.250' AS DateTime), N'<div class="reply-comment-section"><div class="row"><div class="col-xs-12"><div class="post-comment-section"><p id="pBlogReplyCommentorName"><span id="sReplyCommentorName" class="blogcommentname">Megha k</span><span id="sBlogReplyCommentDate">Nov12,2016 5:38PM</span></p><p class="comment-detail" id="pBlogReplyCommentDetails">This is reply comment</p><div class="comment-reply"><ul><li><img class="contributors-list" src="assets/svg/components/blog-detail/icon_reply.svg" alt="Reply comment icon"><a class="joinus-link" id="btnCommentReply" href="#" target="_self">::blogdetailsReply</a></li></ul></div></div></div></div></div>', 0, 1, N'megha.k@sonata-software.com', NULL, NULL)
GO
INSERT [dbo].[Blog_CommentDetails] ([CommentID], [CommentUserName], [CommentText], [CommentApprovedByUserID], [CommentPostDocumentID], [CommentDate], [HtmlComment], [CommentIsSpam], [CommentIsApproved], [CommentEmail], [CommentInfo], [ReplyCommentID]) VALUES (24, N'Siddappa H', N'This is reply text.', NULL, 1, CAST(N'2016-11-12 17:39:58.847' AS DateTime), N'<div class="main-comment-section"><div class="row"><div class="col-xs-12"><div class="post-comment-section"><p id="pCommentorName"><span id="sCommentorName" class="blogcommentname">Siddappa H</span><span id="sBlogPostedDate">Nov12,2016 5:39PM</span></p><p class="comment-detail" id="pBlogCommentDetails">This is reply text.</p><div class="comment-reply"><ul><li><img src="assets/svg/components/blog-detail/icon_reply.svg" alt="Reply comment icon"><a class="joinus-link" id="btnComment" href="#" target="_self">::blogdetailsReply</a></li></ul></div></div></div></div></div>', 0, 1, N'siddappa.h@sonata-software.com', NULL, 23)
GO
INSERT [dbo].[Blog_CommentDetails] ([CommentID], [CommentUserName], [CommentText], [CommentApprovedByUserID], [CommentPostDocumentID], [CommentDate], [HtmlComment], [CommentIsSpam], [CommentIsApproved], [CommentEmail], [CommentInfo], [ReplyCommentID]) VALUES (25, N'Suresh P', N'This is reply comment', NULL, 1, CAST(N'2016-11-12 17:40:44.470' AS DateTime), N'<div class="reply-comment-section"><div class="row"><div class="col-xs-12"><div class="post-comment-section"><p id="pBlogReplyCommentorName"><span id="sReplyCommentorName" class="blogcommentname">Suresh P</span><span id="sBlogReplyCommentDate">Nov12,2016 5:40PM</span></p><p class="comment-detail" id="pBlogReplyCommentDetails">This is reply comment</p><div class="comment-reply"><ul><li><img class="contributors-list" src="assets/svg/components/blog-detail/icon_reply.svg" alt="Reply comment icon"><a class="joinus-link" id="btnCommentReply" href="#" target="_self">::blogdetailsReply</a></li></ul></div></div></div></div></div>', 0, 1, N'suresh.p@sonata-software.com', NULL, NULL)
GO
INSERT [dbo].[Blog_CommentDetails] ([CommentID], [CommentUserName], [CommentText], [CommentApprovedByUserID], [CommentPostDocumentID], [CommentDate], [HtmlComment], [CommentIsSpam], [CommentIsApproved], [CommentEmail], [CommentInfo], [ReplyCommentID]) VALUES (26, N'Vikash', N'This is reply text', NULL, 1, CAST(N'2016-11-12 17:41:44.673' AS DateTime), N'<div class="reply-comment-section"><div class="row"><div class="col-xs-12"><div class="post-comment-section"><p id="pBlogReplyCommentorName"><span id="sReplyCommentorName" class="blogcommentname">Vikash</span><span id="sBlogReplyCommentDate">Nov12,2016 5:40PM</span></p><p class="comment-detail" id="pBlogReplyCommentDetails">This is reply text</p><div class="comment-reply"><ul><li><img class="contributors-list" src="assets/svg/components/blog-detail/icon_reply.svg" alt="Reply comment icon"><a class="joinus-link" id="btnCommentReply" href="#" target="_self">::blogdetailsReply</a></li></ul></div></div></div></div></div>', 0, 1, N'vikash.kr@sonata-software.com', NULL, 23)
GO
SET IDENTITY_INSERT [dbo].[Blog_CommentDetails] OFF
GO
欢迎所有建议!
我也在下面添加了三个插入查询:
Valex,请将此查询插入到表中:“INSERT [dbo].[Blog_CommentDetails] ([CommentID], [CommentUserName], [CommentText], [CommentApprovedByUserID], [CommentPostDocumentID], [CommentDate], [HtmlComment ]、[CommentIsSpam]、[CommentIsApproved]、[CommentEmail]、[CommentInfo]、[ReplyCommentID]、[IsRejected])值(58、N'Vicky'、N'Test'、0、1、CAST(N'2016- 12-02 11:51:07.270' AS DateTime), N'VickyDec2,2016 11:47AM
测试我使用了以下查询:
WITH CTE AS ( SELECT CommentID ,
CommentPostDocumentID ,
CommentIsApproved,
CommentDate ,
ReplyCommentID ,
CommentUserName,
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.CommentPostDocumentID ,
t.CommentIsApproved,
T.CommentDate ,
T.ReplyCommentID ,
T.CommentUserName,
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 = 18 AND CommentIsApproved=1
ORDER BY ThreadID ,
PathStr ,
CommentDate DESC
以下是预期的结构:
最佳答案
如果您想要完整的层次结构,甚至是其中的一部分,并具有正确的顺序
EDIT - Removed Concat() for 2008
Declare @Top int = 23 --null --<< Sets top of Hier Try 23
Declare @Nest varchar(25) ='|-----' --<< Optional: Added for readability
;with cteP as (
Select Seq = cast(1000+Row_Number() over (Order by CommentID) as varchar(500))
,CommentID
,ReplyCommentID
,Lvl=1
From [dbo].[Blog_CommentDetails]
Where IsNull(@Top,-1) = case when @Top is null then isnull(ReplyCommentID,-1) else CommentID end
Union All
Select Seq = cast(p.Seq+'.'+cast(1000+Row_Number() over (Order by r.CommentID) as varchar(25)) as varchar(500))
,r.CommentID
,r.ReplyCommentID
,p.Lvl+1
From [dbo].[Blog_CommentDetails] r
Join cteP p on r.ReplyCommentID = p.CommentID)
,cteR1 as (Select *,R1=Row_Number() over (Order By Seq) From cteP)
,cteR2 as (Select A.Seq,A.CommentID,R2=Max(B.R1) From cteR1 A Join cteR1 B on (B.Seq like A.Seq+'%') Group By A.Seq,A.CommentID )
Select A.R1
,B.R2
,A.CommentID
,A.ReplyCommentID
,A.Lvl
,CommentText = Replicate(@Nest,A.Lvl-1) + C.CommentText
-- Include any other fields from [dbo].[Blog_CommentDetails] alias C
From cteR1 A
Join cteR2 B on A.CommentID=B.CommentID
Join [dbo].[Blog_CommentDetails] C on A.CommentID=C.CommentID
Order By A.R1
返回
如果@Top 设置为 23(例如),则返回结果为
我应该补充一点,cteR2 不是必需的,但它确实表明范围和/或父级或叶级。
关于sql-server - 如何使用 SQL 查询像父子一样显示层次结构行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40563067/
我正在开发我的第一个核心数据支持的应用程序,但无法弄清楚如何正确设置 NSFetchedResultsController。我有两个实体: /-----------\ /--------
我是 javax.swing.* 包的新手,只熟悉 c#.net。我有两个 JFrame,分别是 frmLogin 和 frmMain。我想要做的只是一个像这样的简单代码: +----- C# 版本
我正在从 CoreData 迁移到 Realm...本质上我需要有两个独立的数据库,假设一个只有内存,第二个有磁盘持久性 现在在解析期间,我需要创建一个可以在给定线程中工作但与我选择的顶级 Realm
下面是我的表中的内容。 我的表格 ++++++++++++++++++++ Parent + Child ++++++++++++++++++++ C1 + G1 C1
好的,让我们设置一个场景。 场景: 您有一个内容 Controller 。该内容可能是一张照片、一篇博文等等。现在,在此内容的 html 中,您有一个 Comment Controller 。 Com
我有两个脚本 parent.sh 和 child.sh。 parent.sh 中有一个变量需要被子进程访问。我已经通过在父脚本中导出变量来实现这一点,并且该变量可用于子进程? 有什么方法可以让 chi
我见过类似但不完全相同的请求。 如果我有下表 Parent Child 1 2 1 3 4 3 5 1 6 1 5 7 8
这类似于问题 ( Finding parents in a tree hierarchy for a given child LINQ (lambda expression) )。但是,我不需要找到所
好的,所以我是 C# 做事的新手,我来自 ruby 世界。 我有一个一对多的关系(为了这个问题, parent 对 child ),出于某种原因,L2S 想要创建一个新的 parent 而不是使用
我想为我的网站创建一个完全由数据驱动的面包屑。 数据使用 MariaDB 存储,如下所示: parent_id | parent_name | child_id | child_name ——————
所以,我正在查看 Parse Anypic 教程中的代码 here 我的问题是: 有这 2 个 View Controller : @interface PAPHomeViewController :
我正在使用 CSS 处理树结构层次结构,我需要一些帮助。我想让两个父子链接起来,如果我删除字符 a,我将无法查看 CSS ::after 选择器中定义的 border-left 来自内容元素。 这是我
我对实现以下目标有点困惑1.禁用一个父div的可见性2. 但另一个代码允许子 div 可见性 http://jsfiddle.net/cbXxU/ 如果父 div 不可见或对此
我正在创建一个水平的 ul 导航栏。每个 li 元素都有不同的宽度。我想在下拉菜单中做到这一点,下拉菜单中的“子级” li 项的长度/宽度与“父级” li 项的长度/宽度相同。正在考虑使用 jQuer
我有这个问题很长时间了,我已经在网上和SO进进出出搜索,但还没有找到解决方案。我希望你能帮助我。 我有两个实体之间的父子关系,如下所示: @Entity public class Parent {
我需要在我的网页上找到一个与其他元素具有相同标签的元素,因此我需要根据子部分[parent]的标题找到xpath。 以下是网页的html代码: 基本上,我想根据上图中的 Scheduled 文本在 l
我有 2 张 table .. Adult ------------------- id ParentChild ------------------- parentID(adult's id) ch
我想创建一个函数来创建彼此具有分层关系的对象。因此,每个层对象都拥有自己的一组子层对象,并与其所有兄弟对象共享一个父对象。我不熟悉任何模式,但我想应该有一个模式可以涵盖这种情况。 //construc
我有一个父 div 和一个子 div。这里是 html Parent Child Div Js var parent = document.createElement("parent");
我正在开发一个 reactjs 应用程序 - 我正在分解一个大组件以拥有一个子组件。我在 child 中创建了一个回调函数,它将返回给 parent 。当一个复选框被选中时——子组件执行回调并返回到父
我是一名优秀的程序员,十分优秀!