sql 查询语句。

细嗅蔷薇 2013-05-14 11:00:29

CREATE TABLE [ExpertReply](
[ID] [int] NOT NULL,
[UserID] [int] NOT NULL,
[BlogID] [int] NOT NULL,
[CommentID] [int] NOT NULL,
[Context] [nvarchar](max) NULL,
[CreateTime] [datetime] NOT NULL,
)



insert ExpertReply values(1, 1, 1, 0, '回复1', GETDATE())
insert ExpertReply values(2, 2, 1, 0, '回复2', GETDATE())
insert ExpertReply values(3, 3, 1, 0, '回复3', GETDATE())
insert ExpertReply values(4, 4, 1, 2, '评论1', GETDATE())
insert ExpertReply values(5, 3, 1, 3, '评论3', GETDATE())
insert ExpertReply values(6, 2, 1, 2, '评论2', GETDATE())
insert ExpertReply values(7, 3, 1, 3, '评论4', GETDATE())

想要的效果是:如果这条回复有评论,那么下一条数据就是他的评论
例如:
回复1
回复2
评论1
评论2
回复3
评论3
评论4
回复4
.....

下面的这条语句效果不太理想,有朋友能帮忙修改,或者提出更好的方法么?

select * from ExpertReply order by case when CommentID!=0 then CommentID else id end
...全文
199 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
發糞塗牆 2013-09-16
  • 打赏
  • 举报
回复
引用 18 楼 lovesheng1212 的回复:
[quote=引用 15 楼 DBA_Huangzj 的回复:] ;WITH cte AS ( SELECT [ID] , [UserID] , [BlogID] , [CommentID] , [Context], [CreateTime],id AS parentid FROM ExpertReply WHERE CommentID=0 UNION ALL SELECT a.id,a.[UserID],a.blogid,a.commentid,a.context,a.createtime,b.id AS parentid FROM ExpertReply a INNER JOIN cte b ON a.CommentID=b.id ) SELECT TOP 99.99 PERCENT [ID] , [UserID] , [BlogID] , [CommentID] , [Context], [CreateTime] FROM cte ORDER BY parentid
如果我需要按时间排序应该如何做?[/quote]order by那里改一下就可以了
细嗅蔷薇 2013-09-16
  • 打赏
  • 举报
回复
引用 15 楼 DBA_Huangzj 的回复:
;WITH cte AS ( SELECT [ID] , [UserID] , [BlogID] , [CommentID] , [Context], [CreateTime],id AS parentid FROM ExpertReply WHERE CommentID=0 UNION ALL SELECT a.id,a.[UserID],a.blogid,a.commentid,a.context,a.createtime,b.id AS parentid FROM ExpertReply a INNER JOIN cte b ON a.CommentID=b.id ) SELECT TOP 99.99 PERCENT [ID] , [UserID] , [BlogID] , [CommentID] , [Context], [CreateTime] FROM cte ORDER BY parentid
如果我需要按时间排序应该如何做?
细嗅蔷薇 2013-09-16
  • 打赏
  • 举报
回复
回复需要最新的现在在最上面。评论跟在回复下面就行了。不做要求。
發糞塗牆 2013-09-16
  • 打赏
  • 举报
回复
但是评论的createtime应该比回复的要大啊
细嗅蔷薇 2013-09-16
  • 打赏
  • 举报
回复
引用 21 楼 DBA_Huangzj 的回复:
;WITH cte AS ( SELECT [ID] , [UserID] , [BlogID] , [CommentID] , [Context], [CreateTime],id AS parentid FROM ExpertReply WHERE CommentID=0 UNION ALL SELECT a.id,a.[UserID],a.blogid,a.commentid,a.context,a.createtime,b.id AS parentid FROM ExpertReply a INNER JOIN cte b ON a.CommentID=b.id ) SELECT TOP 99.99 PERCENT [ID] , [UserID] , [BlogID] , [CommentID] , [Context], [CreateTime] FROM cte ORDER BY parentid,createtime 这样呢?
评论就跑到回复的上面去了。。。。
發糞塗牆 2013-09-16
  • 打赏
  • 举报
回复
;WITH cte AS ( SELECT [ID] , [UserID] , [BlogID] , [CommentID] , [Context], [CreateTime],id AS parentid FROM ExpertReply WHERE CommentID=0 UNION ALL SELECT a.id,a.[UserID],a.blogid,a.commentid,a.context,a.createtime,b.id AS parentid FROM ExpertReply a INNER JOIN cte b ON a.CommentID=b.id ) SELECT TOP 99.99 PERCENT [ID] , [UserID] , [BlogID] , [CommentID] , [Context], [CreateTime] FROM cte ORDER BY parentid,createtime 这样呢?
细嗅蔷薇 2013-09-16
  • 打赏
  • 举报
回复
引用 19 楼 DBA_Huangzj 的回复:
[quote=引用 18 楼 lovesheng1212 的回复:] [quote=引用 15 楼 DBA_Huangzj 的回复:] ;WITH cte AS ( SELECT [ID] , [UserID] , [BlogID] , [CommentID] , [Context], [CreateTime],id AS parentid FROM ExpertReply WHERE CommentID=0 UNION ALL SELECT a.id,a.[UserID],a.blogid,a.commentid,a.context,a.createtime,b.id AS parentid FROM ExpertReply a INNER JOIN cte b ON a.CommentID=b.id ) SELECT TOP 99.99 PERCENT [ID] , [UserID] , [BlogID] , [CommentID] , [Context], [CreateTime] FROM cte ORDER BY parentid
如果我需要按时间排序应该如何做?[/quote]order by那里改一下就可以了[/quote] 这样 评论就跑到回复的上面去了。。。。
發糞塗牆 2013-05-14
  • 打赏
  • 举报
回复
--CREATE TABLE [ExpertReply]( 
--    [ID] [int] NOT NULL, 
--    [UserID] [int] NOT NULL, 
--    [BlogID] [int] NOT NULL, 
--    [CommentID] [int] NOT NULL, 
--    [Context] [nvarchar](max) NULL, 
--    [CreateTime] [datetime] NOT NULL, 
--) 
   
  
   
--insert ExpertReply values(1, 1, 1, 0, '回复1', GETDATE()) 
--insert ExpertReply values(2, 2, 1, 0, '回复2', GETDATE()) 
--insert ExpertReply values(3, 3, 1, 0, '回复3',  GETDATE()) 
--insert ExpertReply values(4, 4, 1, 2, '评论1',  GETDATE()) 
--insert ExpertReply values(5, 3, 1, 3, '评论3',  GETDATE()) 
--insert ExpertReply values(6, 2, 1, 2, '评论2',  GETDATE()) 
----insert ExpertReply values(7, 3, 1, 3, '评论4',  GETDATE())
;WITH cte AS (
SELECT id,CommentID,context,id AS parentid
FROM ExpertReply
WHERE CommentID=0
UNION ALL 
SELECT a.id,a.commentid,a.context,b.id AS parentid
FROM ExpertReply a INNER JOIN cte b ON a.CommentID=b.id
)
SELECT id,CommentID,context FROM cte
ORDER BY parentid

/*
id          CommentID   context
----------- ----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1           0           回复1
2           0           回复2
4           2           评论1
6           2           评论2
3           0           回复3
5           3           评论3
7           3           评论4

*/
哥眼神纯洁不 2013-05-14
  • 打赏
  • 举报
回复

with tb as(select id,commentid=id,context from  [ExpertReply] where  commentid=0
union all
select  a.id,tb.commentid,a.context   from tb,[ExpertReply] a where tb.id=a.commentid
)
select context from tb
order by commentid,id
这样OK吗?
CHQIUU 2013-05-14
  • 打赏
  • 举报
回复
你这个也太麻烦了吧,为什么不把回复和评论单独来标识呢? 我理解的没错的话,你这儿回复的全部标识的为0
细嗅蔷薇 2013-05-14
  • 打赏
  • 举报
回复
感谢各位!。
细嗅蔷薇 2013-05-14
  • 打赏
  • 举报
回复
是创建视图会出错。 消息 1033,级别 15,状态 1,过程 V_ExpertReplyBytheme,第 12 行 除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。
發糞塗牆 2013-05-14
  • 打赏
  • 举报
回复
;WITH cte AS ( SELECT [ID] , [UserID] , [BlogID] , [CommentID] , [Context], [CreateTime],id AS parentid FROM ExpertReply WHERE CommentID=0 UNION ALL SELECT a.id,a.[UserID],a.blogid,a.commentid,a.context,a.createtime,b.id AS parentid FROM ExpertReply a INNER JOIN cte b ON a.CommentID=b.id ) SELECT TOP 99.99 PERCENT [ID] , [UserID] , [BlogID] , [CommentID] , [Context], [CreateTime] FROM cte ORDER BY parentid
哥眼神纯洁不 2013-05-14
  • 打赏
  • 举报
回复

with tb as
(
select id,userid,blogid,commentid as old_commentid,context,createtime,commentid=id 
from  [ExpertReply] where  commentid=0
union all
select  a.id,a.userid,a.BlogID,a.CommentID,a.context,a.CreateTime,tb.commentid   
from tb,[ExpertReply] a where tb.id=a.commentid
)
select ID,UserID,BlogID,old_commentid commetnid,Context,CreateTime,commentid as orderby 
from tb
order by commentid,id
这种效果?
细嗅蔷薇 2013-05-14
  • 打赏
  • 举报
回复
引用 9 楼 DBA_Huangzj 的回复:
[quote=引用 7 楼 lovesheng1212 的回复:] 不好意思,你的语句查询测试表的时候显示正确,但是放到正式的表里查询就会出问题,不知道为什么。我在看看吧,这个语句可以直接转成view么
可以写成视图,直接 create view xxx as ;WITH cte AS ( SELECT id,CommentID,context,id AS parentid FROM ExpertReply WHERE CommentID=0 UNION ALL SELECT a.id,a.commentid,a.context,b.id AS parentid FROM ExpertReply a INNER JOIN cte b ON a.CommentID=b.id ) SELECT id,CommentID,context FROM cte ORDER BY parentid [/quote] 消息 1033,级别 15,状态 1,过程 V_ExpertReplyBytheme,第 12 行 除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。
發糞塗牆 2013-05-14
  • 打赏
  • 举报
回复
--CREATE TABLE [ExpertReply](  
--    [ID] [int] NOT NULL,  
--    [UserID] [int] NOT NULL,  
--    [BlogID] [int] NOT NULL,  
--    [CommentID] [int] NOT NULL,  
--    [Context] [nvarchar](max) NULL,  
--    [CreateTime] [datetime] NOT NULL,  
--)  
     
    
     
--insert ExpertReply values(1, 1, 1, 0, '回复1', GETDATE())  
--insert ExpertReply values(2, 2, 1, 0, '回复2', GETDATE())  
--insert ExpertReply values(3, 3, 1, 0, '回复3',  GETDATE())  
--insert ExpertReply values(4, 4, 1, 2, '评论1',  GETDATE())  
--insert ExpertReply values(5, 3, 1, 3, '评论3',  GETDATE())  
--insert ExpertReply values(6, 2, 1, 2, '评论2',  GETDATE())  
----insert ExpertReply values(7, 3, 1, 3, '评论4',  GETDATE()) 
;WITH cte AS ( 
SELECT [ID]  ,   [UserID] ,    [BlogID] ,  [CommentID] ,    [Context],    [CreateTime],id AS parentid 
FROM ExpertReply 
WHERE CommentID=0 
UNION ALL 
SELECT a.id,a.[UserID],a.blogid,a.commentid,a.context,a.createtime,b.id AS parentid 
FROM ExpertReply a INNER JOIN cte b ON a.CommentID=b.id 
) 
SELECT [ID]  ,   [UserID] ,    [BlogID] ,  [CommentID] ,    [Context],    [CreateTime]  FROM cte 
ORDER BY parentid 
  
/* 
ID          UserID      BlogID      CommentID   Context                                                                                                                                                                                                                                                          CreateTime
----------- ----------- ----------- ----------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------------------
1           1           1           0           回复1                                                                                                                                                                                                                                                              2013-05-14 11:56:16.150
2           2           1           0           回复2                                                                                                                                                                                                                                                              2013-05-14 11:56:16.150
4           4           1           2           评论1                                                                                                                                                                                                                                                              2013-05-14 11:56:16.150
6           2           1           2           评论2                                                                                                                                                                                                                                                              2013-05-14 11:56:16.150
3           3           1           0           回复3                                                                                                                                                                                                                                                              2013-05-14 11:56:16.150
5           3           1           3           评论3                                                                                                                                                                                                                                                              2013-05-14 11:56:16.150
7           3           1           3           评论4     
  
*/
细嗅蔷薇 2013-05-14
  • 打赏
  • 举报
回复
引用 10 楼 sc273607742 的回复:
[quote=引用 8 楼 lovesheng1212 的回复:] [quote=引用 2 楼 sc273607742 的回复:]

with tb as(select id,commentid=id,context from  [ExpertReply] where  commentid=0
union all
select  a.id,tb.commentid,a.context   from tb,[ExpertReply] a where tb.id=a.commentid
)
select context from tb
order by commentid,id
这样OK吗?
为什么我显示全字段的时候 就不行了?[/quote] 我这个和大版的思路一样...你如果有啥特殊需求,多放点数据来看看,你光这么说,谁也不明白..[/quote] 就是显示所有的字段。 [ID] , [UserID] , [BlogID] , [CommentID] , [Context], [CreateTime]
哥眼神纯洁不 2013-05-14
  • 打赏
  • 举报
回复
引用 8 楼 lovesheng1212 的回复:
[quote=引用 2 楼 sc273607742 的回复:]

with tb as(select id,commentid=id,context from  [ExpertReply] where  commentid=0
union all
select  a.id,tb.commentid,a.context   from tb,[ExpertReply] a where tb.id=a.commentid
)
select context from tb
order by commentid,id
这样OK吗?
为什么我显示全字段的时候 就不行了?[/quote] 我这个和大版的思路一样...你如果有啥特殊需求,多放点数据来看看,你光这么说,谁也不明白..
發糞塗牆 2013-05-14
  • 打赏
  • 举报
回复
引用 7 楼 lovesheng1212 的回复:
不好意思,你的语句查询测试表的时候显示正确,但是放到正式的表里查询就会出问题,不知道为什么。我在看看吧,这个语句可以直接转成view么
可以写成视图,直接 create view xxx as ;WITH cte AS ( SELECT id,CommentID,context,id AS parentid FROM ExpertReply WHERE CommentID=0 UNION ALL SELECT a.id,a.commentid,a.context,b.id AS parentid FROM ExpertReply a INNER JOIN cte b ON a.CommentID=b.id ) SELECT id,CommentID,context FROM cte ORDER BY parentid
细嗅蔷薇 2013-05-14
  • 打赏
  • 举报
回复
引用 2 楼 sc273607742 的回复:

with tb as(select id,commentid=id,context from  [ExpertReply] where  commentid=0
union all
select  a.id,tb.commentid,a.context   from tb,[ExpertReply] a where tb.id=a.commentid
)
select context from tb
order by commentid,id
这样OK吗?
为什么我显示全字段的时候 就不行了?
加载更多回复(4)

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧