我设计了一个评论表,里面增加了一个评论字段,用于记录当前评论是对之前的那条回复做评论,但现在在做排序时,没法把这个评论插在回复之前。USE [tempdb]
GOdrop table [BlogComment]
CREATE TABLE [BlogComment](
[ID] [int] NOT NULL,
[UserID] [int] NOT NULL,
[BlogID] [int] NOT NULL,
[CommentID] [int] NOT NULL,
[Context] [nvarchar](max) NULL,
[CreateTime] [datetime] NOT NULL,
)GOinsert BlogComment values(1, 1, 1, 0, '回复1', '2010-3-21')
insert BlogComment values(2, 2, 1, 0, '回复2', '2010-3-22')
insert BlogComment values(3, 3, 1, 0, '回复3', '2010-3-23')
insert BlogComment values(4, 4, 1, 2, '回复2-评论1', '2010-3-24')
insert BlogComment values(5, 3, 1, 0, '回复4', '2010-3-25')
insert BlogComment values(6, 2, 1, 2, '回复2-评论2', '2010-3-26')select * from BlogComment where BlogID = 1 order by CreateTime asc现在的查询语句的结果是:ID UserID BlogID CommentID Context CreateTime
1 1 1 0 回复1 2010-03-21 00:00:00.000
2 2 1 0 回复2 2010-03-22 00:00:00.000
3 3 1 0 回复3 2010-03-23 00:00:00.000
4 4 1 2 回复2-评论1 2010-03-24 00:00:00.000
5 3 1 0 回复4 2010-03-25 00:00:00.000
6 2 1 2 回复2-评论2 2010-03-26 00:00:00.000
而我需要的返回是,对“回复2”的两条“评论”应该紧跟着“回复2”后面返回:
ID UserID BlogID CommentID Context CreateTime
1 1 1 0 回复1 2010-03-21 00:00:00.000
2 2 1 0 回复2 2010-03-22 00:00:00.000
4 4 1 2 回复2-评论1 2010-03-24 00:00:00.000
6 2 1 2 回复2-评论2 2010-03-26 00:00:00.000
3 3 1 0 回复3 2010-03-23 00:00:00.000
5 3 1 0 回复4 2010-03-25 00:00:00.000
GOdrop table [BlogComment]
CREATE TABLE [BlogComment](
[ID] [int] NOT NULL,
[UserID] [int] NOT NULL,
[BlogID] [int] NOT NULL,
[CommentID] [int] NOT NULL,
[Context] [nvarchar](max) NULL,
[CreateTime] [datetime] NOT NULL,
)GOinsert BlogComment values(1, 1, 1, 0, '回复1', '2010-3-21')
insert BlogComment values(2, 2, 1, 0, '回复2', '2010-3-22')
insert BlogComment values(3, 3, 1, 0, '回复3', '2010-3-23')
insert BlogComment values(4, 4, 1, 2, '回复2-评论1', '2010-3-24')
insert BlogComment values(5, 3, 1, 0, '回复4', '2010-3-25')
insert BlogComment values(6, 2, 1, 2, '回复2-评论2', '2010-3-26')select * from BlogComment where BlogID = 1 order by CreateTime asc现在的查询语句的结果是:ID UserID BlogID CommentID Context CreateTime
1 1 1 0 回复1 2010-03-21 00:00:00.000
2 2 1 0 回复2 2010-03-22 00:00:00.000
3 3 1 0 回复3 2010-03-23 00:00:00.000
4 4 1 2 回复2-评论1 2010-03-24 00:00:00.000
5 3 1 0 回复4 2010-03-25 00:00:00.000
6 2 1 2 回复2-评论2 2010-03-26 00:00:00.000
而我需要的返回是,对“回复2”的两条“评论”应该紧跟着“回复2”后面返回:
ID UserID BlogID CommentID Context CreateTime
1 1 1 0 回复1 2010-03-21 00:00:00.000
2 2 1 0 回复2 2010-03-22 00:00:00.000
4 4 1 2 回复2-评论1 2010-03-24 00:00:00.000
6 2 1 2 回复2-评论2 2010-03-26 00:00:00.000
3 3 1 0 回复3 2010-03-23 00:00:00.000
5 3 1 0 回复4 2010-03-25 00:00:00.000
解决方案 »
- excel导入sql提示外部表不是预期的格式
- 请问如何让一个表中的一列等于另一列 我只有20分
- 事务隔离级别
- sql查询的问题--xsda表中有姓名列,按姓分组怎么写?
- 有高手能帮我写个存储语句举取得这个功能呢
- sql 时间截取
- 请问在sql server中那里可以看到用户名和密码?
- UNION ALL 视图 'tb' 不可更新,因为没有找到分区列
- sql server 2000仲的money数据类型与decimail、numeric之间,有什么大的区别??
- 关于连接sqlserver的奇怪问题
- 各位大虾,关于ttf160_tlb.pas,ttf160_tlb.dcu问题
- sql server里如何给子查询设置别名
select * from BlogComment where BlogID = 1 order by Context,CreateTime ascID UserID BlogID CommentID Context CreateTime
----------- ----------- ----------- ----------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------------------
1 1 1 0 回复1 2010-03-21 00:00:00.000
2 2 1 0 回复2 2010-03-22 00:00:00.000
4 4 1 2 回复2-评论1 2010-03-24 00:00:00.000
6 2 1 2 回复2-评论2 2010-03-26 00:00:00.000
3 3 1 0 回复3 2010-03-23 00:00:00.000
5 3 1 0 回复4 2010-03-25 00:00:00.000(6 行受影响)
from BlogComment
order by case when CommentID!=0 then CommentID else id end
CREATE TABLE [BlogComment](
[ID] [int] NOT NULL,
[UserID] [int] NOT NULL,
[BlogID] [int] NOT NULL,
[CommentID] [int] NOT NULL,
[Context] [nvarchar](max) NULL,
[CreateTime] [datetime] NOT NULL,
)GOinsert BlogComment values(1, 1, 1, 0, '回复1', '2010-3-21')
insert BlogComment values(2, 2, 1, 0, '回复2', '2010-3-22')
insert BlogComment values(3, 3, 1, 0, '回复3', '2010-3-23')
insert BlogComment values(4, 4, 1, 2, '回复2-评论1', '2010-3-24')
insert BlogComment values(5, 3, 1, 0, '回复4', '2010-3-25')
insert BlogComment values(6, 2, 1, 2, '回复2-评论2', '2010-3-26')select * from BlogComment where BlogID = 1 order by Context,CreateTime,len(Context) ascdrop table BlogComment
/*ID UserID BlogID CommentID Context CreateTime
----------- ----------- ----------- ----------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------------------
1 1 1 0 回复1 2010-03-21 00:00:00.000
2 2 1 0 回复2 2010-03-22 00:00:00.000
4 4 1 2 回复2-评论1 2010-03-24 00:00:00.000
6 2 1 2 回复2-评论2 2010-03-26 00:00:00.000
3 3 1 0 回复3 2010-03-23 00:00:00.000
5 3 1 0 回复4 2010-03-25 00:00:00.000(6 行受影响)
*/
ID UserID BlogID CommentID Context CreateTime
----------- ----------- ----------- ----------- -------------------- ------------------------------------------------------
1 1 1 0 回复1 2010-03-21 00:00:00.000
2 2 1 0 回复2 2010-03-22 00:00:00.000
4 4 1 2 回复2-评论1 2010-03-24 00:00:00.000
6 2 1 2 回复2-评论2 2010-03-26 00:00:00.000
3 3 1 0 回复3 2010-03-23 00:00:00.000
5 3 1 0 回复4 2010-03-25 00:00:00.000(所影响的行数为 6 行)*/
[ID] [int] NOT NULL,
[UserID] [int] NOT NULL,
[BlogID] [int] NOT NULL,
[CommentID] [int] NOT NULL,
[Context] [nvarchar](max) NULL,
[CreateTime] [datetime] NOT NULL,
)GOinsert BlogComment values(1, 1, 1, 0, '回复1', '2010-3-21')
insert BlogComment values(2, 2, 1, 0, '回复2', '2010-3-22')
insert BlogComment values(3, 3, 1, 0, '回复3', '2010-3-23')
insert BlogComment values(4, 4, 1, 2, '回复2-评论1', '2010-3-24')
insert BlogComment values(5, 3, 1, 0, '回复4', '2010-3-25')
insert BlogComment values(6, 2, 1, 2, '回复2-评论2', '2010-3-26')
select * from BlogComment where BlogID = 1 order by ContextID UserID BlogID CommentID Context CreateTime
----------- ----------- ----------- ----------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------------------
1 1 1 0 回复1 2010-03-21 00:00:00.000
2 2 1 0 回复2 2010-03-22 00:00:00.000
4 4 1 2 回复2-评论1 2010-03-24 00:00:00.000
6 2 1 2 回复2-评论2 2010-03-26 00:00:00.000
3 3 1 0 回复3 2010-03-23 00:00:00.000
5 3 1 0 回复4 2010-03-25 00:00:00.000(6 行受影响)