出于兴趣,近期做了一个图片分享的小项目,其中在做有关图片的评论以及回复的功能时,刚开始对于这中功能有一些纠结,纠结的是评论与回复的内容是放在两个表还是一个表中,对于放在两张表的结构考虑到后期的数据读取的复杂问题,最后决定将评论和回复的功能都放在同一张表中。数据库采用SqlServer,具体表设计如下:
1 CREATE TABLE [dbo].[DemoComment]( 2 [RowGuid] [nvarchar](50) NOT NULL, 3 [ParentGuid] [nvarchar](50) NULL, 4 [CommentText] [nvarchar](200) NULL, 5 [CommentUserGuid] [nvarchar](50) NULL, 6 [CommentUserName] [nvarchar](50) NULL, 7 [CommentDate] [datetime] NULL, 8 [ToUserGuid] [nvarchar](50) NULL, 9 [ToUserName] [nvarchar](50) NULL, 10 [CommentPictureGuid] [nvarchar](50) NULL 11 )
然后对每一张图片的评论就会有两种情况:1、评论。2、评论和回复。现假设有"person_A"和"person_B"两人对图片"pic"评论,则有
1、评论情况就是简单的插入一条评论记录:
1 insert into DemoComment(RowGuid,CommentText,CommentUserGuid,CommentUserName,CommentDate,CommentPictureGuid) 2 values(NEWID(),'wow,nice pic!','person_A_Guid','person_A',getdate(),'pic'); 3 insert into DemoComment(RowGuid,CommentText,CommentUserGuid,CommentUserName,CommentDate,CommentPictureGuid) 4 values(NEWID(),'wow,what a nice pic!','person_B_Guid','person_B',getdate(),'pic');
则用户浏览图片详细时,可以看到图片“pic”下有如下两条评论:
此时,有“person_C”是“person_B”的好友,“person_C”在“person_B”的评论下进行了回复,则有
2、“person_C”对“person_B”评论的回复
1 insert into DemoComment(RowGuid,ParentGuid,CommentText,CommentUserGuid,CommentUserName,CommentDate,ToUserGuid,ToUserName,CommentPictureGuid) 2 values(NEWID(),'E07E9026-0194-4695-9FE4-FDD4DF9D3865','yes,I want to get one!','person_C_Guid','person_C',getdate(),'person_B_Guid','person_B','pic');
然后“person_B”也对“person_C”进行了回复
1 insert into DemoComment(RowGuid,ParentGuid,CommentText,CommentUserGuid,CommentUserName,CommentDate,ToUserGuid,ToUserName,CommentPictureGuid) 2 values(NEWID(),'B688AB26-22D8-42BF-A518-10E3EFDC041F','OK,I will buy one for you!','person_B_Guid','person_B',getdate(),'person_C_Guid','person_C','pic');
然后“person_C”也对“person_B”表达了谢意。
1 insert into DemoComment(RowGuid,ParentGuid,CommentText,CommentUserGuid,CommentUserName,CommentDate,ToUserGuid,ToUserName,CommentPictureGuid) 2 values(NEWID(),'81E882B3-9232-40C7-8BBD-F1E821063B64','really,Thank you very muck!','person_C_Guid','person_C',getdate(),'person_B_Guid','person_B','pic');
至此,所有的评论与回复已全部完成。
现在要做的就是取出对“pic”的所有评论和回复。
注意,此时可不是简单地按照时间进行排序,假设“person_A”开始做了5条评论,过了一段时间"person_B"对“person_A”的第一条评论进行了回复,如果按时间排序肯定出错。这是就可以看到“RowGuid”和“ParentGuid”的关系了:
没错,RowGuid和ParentGuid具有关联关系,我们要做的就是找到第一条评论的RowGuid,然后与之关联的评论与回复就可以全部取出了。这里有一点递归的意思。
定义一个存储过程,取出某一条评论下的所有回复:
1 create procedure [dbo].[sp_GetCommnetsByRowGuid] (@RowGuid nvarchar(50)) 2 as 3 begin 4 declare @comment table --定义表变量 5 ( 6 RowGuid nvarchar(50) not null default NEWID(), 7 ParentGuid nvarchar(50) null, 8 CommentText nvarchar(200) null, 9 CommentUserGuid nvarchar(50) null, 10 CommentUserName nvarchar(50) null, 11 CommentDate datetime null, 12 ToUserGuid nvarchar(50) null, 13 ToUserName nvarchar(50) null, 14 PictureGuid nvarchar(50) null 15 ) 16 declare @parentGuid nvarchar(50);--第一父表标识变量 17 18 insert into @comment 19 select d.* from DemoComment d 20 where d.RowGuid=@RowGuid--添加记录到表变量 21 22 --select @parentGuid=c.RowGuid from @comment c; 23 set @parentGuid=@RowGuid;--初始化父表标识的值 24 declare @count int; 25 set @count=1;--初始化循环条件,默认为1,标识可以循环 26 while @count >0 27 begin 28 insert into @comment select * from DemoComment where parentguid=@parentGuid;--增加一条记录到表变量 29 select @parentGuid=c.RowGuid from DemoComment c where c.parentguid=@parentGuid;--修改父表标识的值 30 select @count=COUNT(RowGuid) from DemoComment c where c.parentguid=@parentGuid;--为循环条件赋值 31 end 32 33 select c.* from @comment c; 34 end 35 36 37 GO
执行该存储过程有结果如下:
好了,现在可以再写一个存储过程调用“[dbo].[sp_GetCommnetsByRowGuid] ”一次取出所有评论:
1 create procedure sp_GetCommentsAndReplys 2 as 3 begin 4 declare @RowNumberMin int; 5 declare @RowNumberMax int; 6 7 select @RowNumberMin=MIN(t.RowNumber),@RowNumberMax=MAX(t.RowNumber) 8 from (select ROW_NUMBER()over(order by d.commentdate) RowNumber,d.* 9 from DemoComment d 10 where ParentGuid is null or ParentGuid ='')t 11 12 print @RowNumberMin; 13 print @RowNumberMax; 14 15 16 declare @comment table --定义表变量 17 ( 18 RowGuid nvarchar(50) not null default NEWID(), 19 ParentGuid nvarchar(50) null, 20 CommentText nvarchar(200) null, 21 CommentUserGuid nvarchar(50) null, 22 CommentUserName nvarchar(50) null, 23 CommentDate datetime null, 24 ToUserGuid nvarchar(50) null, 25 ToUserName nvarchar(50) null, 26 PictureGuid nvarchar(50) null 27 ) 28 29 while @RowNumberMin <=@RowNumberMax 30 begin 31 32 declare @CommentGuid nvarchar(50); 33 34 --insert into @comment 35 select @CommentGuid= t.RowGuid 36 from (select ROW_NUMBER()over(order by d.ParentGuid) RowNumber,d.* 37 from DemoComment d 38 where ParentGuid is null or ParentGuid ='')t 39 where t.RowNumber=@RowNumberMin; 40 41 insert into @comment 42 exec [dbo].[sp_GetCommnetsByRowGuid] @CommentGuid 43 44 set @RowNumberMin=@RowNumberMin+1; 45 end 46 47 select * from @comment; 48 end 49 go 50 exec sp_GetCommentsAndReplys
结果:
好了,功能已完成,之后可以考虑优化的事了。