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