嵌套评论的数据库表设计


create database NestedCommnets
use NestedCommnets
Create table UserComments(
    Id int not null identity(1, 1),
    ParentId int not null,
    Content nvarchar(100) not null,
    Depth smallint not null,
    Thread nvarchar(max) not null
)

往数据库表中添加如下数据:

评论数据表

其中,Thread字段以"/"分隔,罗列了所有的父级Id,Depth字段显示的是层级。


查询所有的评论:

 select SPACE(u.Depth*6) + u.Content as 评论 from UserComments as u

所有评论


如果希望结合Thread和Depth字段进行排序:

--STR(nExpression [, nLength [, nDecimalPlaces]])返回与指定表达式对应的字符串
--nLength,返回的字符串长度;nDecimalPlaces,返回字符串的小数位数
select 
SPACE(u.Depth*6) + u.Content as 评论,
u.Thread + LTRIM(STR(u.Depth,100,0)) as 排序 
from UserComments as u
order by u.Thread + LTRIM(STR(u.Depth,100,0))
排序后评论

转自:嵌套评论的数据库表设计

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值