Sql 如何使最新的文章总是登在布告栏的顶端
我做了一个公告板 像这样Sql 如何使最新的文章总是登在布告栏的顶端,sql,sql-server,Sql,Sql Server,我做了一个公告板 像这样 id parent_id name depth_fullname 1 0 1depth 1 1depth 1 14 1 2depth 1-2 1depth 1 > 2depth 1-4 *Latest 4 1 2depth 1-1 1depth 1 > 2depth 1-1 10 4 3depth 1-1 1depth 1 > 2depth 1-1 > 3depth 1-1 11 4 3
id parent_id name depth_fullname
1 0 1depth 1 1depth 1
14 1 2depth 1-2 1depth 1 > 2depth 1-4 *Latest
4 1 2depth 1-1 1depth 1 > 2depth 1-1
10 4 3depth 1-1 1depth 1 > 2depth 1-1 > 3depth 1-1
11 4 3depth 1-2 1depth 1 > 2depth 1-1 > 3depth 1-2
12 4 3depth 1-3 1depth 1 > 2depth 1-1 > 3depth 1-3
5 1 2depth 1-2 1depth 1 > 2depth 1-2
6 1 2depth 1-3 1depth 1 > 2depth 1-3
2 0 1depth 2 1depth 2
7 2 2depth 2-1 1depth 2 > 2depth 2-1
8 2 2depth 2-2 1depth 2 > 2depth 2-2
9 2 2depth 2-3 1depth 2 > 2depth 2-3
3 0 1depth 3 1depth 3
13 3 2depth 1-1 1depth 3 > 2depth 1-1
sort_number id parent_id name
-------------------- ----------- ----------- ------------------------------
1 1 0 1Title
1 6 1 ㄴRE 1Title 1-3
1 15 6 ㄴRE 3Title 1-3-1
1 14 1 ㄴRE 1Title 1-4
1 4 1 ㄴRE 1Title 1-1
1 12 4 ㄴRE 1Title 1-1-3
1 11 4 ㄴRE 1Title 1-1-2
1 10 4 ㄴRE 1Title 1-1-1
1 5 1 ㄴRE 1Title 1-2
2 3 0 3Title
2 13 3 ㄴRE 3Title 1-1
3 2 0 2Title
3 9 2 ㄴRE 2Title 1-3
3 8 2 ㄴRE 2Title 1-2
3 7 2 ㄴRE 2Title 1-1
sort_number id parent_id name
-------------------- ----------- ----------- ------------------------------
1 1 0 1Title
1 6 1 ㄴRE 1Title 1-3
1 15 6 ㄴRE 3Title 1-3-1
1 14 1 ㄴRE 1Title 1-4
1 4 1 ㄴRE 1Title 1-1
1 12 4 ㄴRE 1Title 1-1-3
1 11 4 ㄴRE 1Title 1-1-2
1 10 4 ㄴRE 1Title 1-1-1
1 5 1 ㄴRE 1Title 1-2
2 3 0 3Title
2 13 3 ㄴRE 3Title 1-1
3 2 0 2Title
3 9 2 ㄴRE 2Title 1-3
3 8 2 ㄴRE 2Title 1-2
3 7 2 ㄴRE 2Title 1-1
当数据被插入时,它被放置在父项下
像这样
id parent_id name depth_fullname
1 0 1depth 1 1depth 1
14 1 2depth 1-2 1depth 1 > 2depth 1-4
4 1 2depth 1-1 1depth 1 > 2depth 1-1
10 4 3depth 1-1 1depth 1 > 2depth 1-1 > 3depth 1-1
11 4 3depth 1-2 1depth 1 > 2depth 1-1 > 3depth 1-2
12 4 3depth 1-3 1depth 1 > 2depth 1-1 > 3depth 1-3
5 1 2depth 1-2 1depth 1 > 2depth 1-2
6 1 2depth 1-3 1depth 1 > 2depth 1-3
15 6 3depth 1-1 1depth 1 > 2depth 1-3 > 3depth 1-1 *Latest
2 0 1depth 2 1depth 2
7 2 2depth 2-1 1depth 2 > 2depth 2-1
8 2 2depth 2-2 1depth 2 > 2depth 2-2
9 2 2depth 2-3 1depth 2 > 2depth 2-3
3 0 1depth 3 1depth 3
13 3 2depth 1-1 1depth 3 > 2depth 1-1
如果留下评论,我想把最新的帖子和父帖子放在一起。
像这样
id parent_id name depth_fullname
1 0 1depth 1 1depth 1
6 1 2depth 1-3 1depth 1 > 2depth 1-3
15 6 3depth 1-1 1depth 1 > 2depth 1-3 > 3depth 1-1
14 1 2depth 1-2 1depth 1 > 2depth 1-4
4 1 2depth 1-1 1depth 1 > 2depth 1-1
12 4 3depth 1-3 1depth 1 > 2depth 1-1 > 3depth 1-3
11 4 3depth 1-2 1depth 1 > 2depth 1-1 > 3depth 1-2
10 4 3depth 1-1 1depth 1 > 2depth 1-1 > 3depth 1-1
5 1 2depth 1-2 1depth 1 > 2depth 1-2
3 0 1depth 3 1depth 3
13 3 2depth 1-1 1depth 3 > 2depth 1-1
2 0 1depth 2 1depth 2
9 2 2depth 2-3 1depth 2 > 2depth 2-3
8 2 2depth 2-2 1depth 2 > 2depth 2-2
7 2 2depth 2-1 1depth 2 > 2depth 2-1
和结果
这是我的问题
创建查询
create table tree_table(
id int not null,
parent_id int not null,
name nvarchar(30) not null
);
选择查询
我使用了递归函数
如果按以下方式操作,则不会始终显示最新文章
WITH tree_query AS (
SELECT
id ,
parent_id ,
name ,
convert(varchar(255), id) sort,
convert(varchar(255), name) depth_fullname
FROM tree_table
WHERE
parent_id = 0
UNION ALL
SELECT
B.id ,
B.parent_id ,
B.name ,
convert(varchar(255),
convert(nvarchar,C.sort) + ' > ' + convert(varchar(255), B.id)) sort ,
convert(varchar(255),
convert(nvarchar,C.depth_fullname) + ' > ' + convert(varchar(255), B.name)) depth_fullname
FROM tree_table B,
tree_query C
WHERE
B.parent_id = C.id )
SELECT
id,
parent_id,
name,
depth_fullname
FROM tree_query
ORDER BY sort
我应该如何更改查询?
谢谢你的回答
编辑 @砂光机 谢谢你的回复。但我有几个问题。在您发送的查询中,注释没有排序,因此我留下一个类似这样的问题 您的查询会产生这个不需要的结果
sort_number id parent_id name
-------------------- ----------- ----------- ------------------------------
1 1 0 1Title
1 4 1 ㄴRE 1Title 1-1
1 5 1 ㄴRE 1Title 1-2
1 6 1 ㄴRE 1Title 1-3
1 14 1 ㄴRE 1Title 1-4
1 15 6 ㄴRE 3Title 1-3-1
1 10 4 ㄴRE 1Title 1-1-1
1 11 4 ㄴRE 1Title 1-1-2
1 12 4 ㄴRE 1Title 1-1-3
2 3 0 3Title
2 13 3 ㄴRE 3Title 1-1
3 2 0 2Title
3 7 2 ㄴRE 2Title 1-1
3 8 2 ㄴRE 2Title 1-2
3 9 2 ㄴRE 2Title 1-3
我想要这样的结果
id parent_id name depth_fullname
1 0 1depth 1 1depth 1
14 1 2depth 1-2 1depth 1 > 2depth 1-4 *Latest
4 1 2depth 1-1 1depth 1 > 2depth 1-1
10 4 3depth 1-1 1depth 1 > 2depth 1-1 > 3depth 1-1
11 4 3depth 1-2 1depth 1 > 2depth 1-1 > 3depth 1-2
12 4 3depth 1-3 1depth 1 > 2depth 1-1 > 3depth 1-3
5 1 2depth 1-2 1depth 1 > 2depth 1-2
6 1 2depth 1-3 1depth 1 > 2depth 1-3
2 0 1depth 2 1depth 2
7 2 2depth 2-1 1depth 2 > 2depth 2-1
8 2 2depth 2-2 1depth 2 > 2depth 2-2
9 2 2depth 2-3 1depth 2 > 2depth 2-3
3 0 1depth 3 1depth 3
13 3 2depth 1-1 1depth 3 > 2depth 1-1
sort_number id parent_id name
-------------------- ----------- ----------- ------------------------------
1 1 0 1Title
1 6 1 ㄴRE 1Title 1-3
1 15 6 ㄴRE 3Title 1-3-1
1 14 1 ㄴRE 1Title 1-4
1 4 1 ㄴRE 1Title 1-1
1 12 4 ㄴRE 1Title 1-1-3
1 11 4 ㄴRE 1Title 1-1-2
1 10 4 ㄴRE 1Title 1-1-1
1 5 1 ㄴRE 1Title 1-2
2 3 0 3Title
2 13 3 ㄴRE 3Title 1-1
3 2 0 2Title
3 9 2 ㄴRE 2Title 1-3
3 8 2 ㄴRE 2Title 1-2
3 7 2 ㄴRE 2Title 1-1
sort_number id parent_id name
-------------------- ----------- ----------- ------------------------------
1 1 0 1Title
1 6 1 ㄴRE 1Title 1-3
1 15 6 ㄴRE 3Title 1-3-1
1 14 1 ㄴRE 1Title 1-4
1 4 1 ㄴRE 1Title 1-1
1 12 4 ㄴRE 1Title 1-1-3
1 11 4 ㄴRE 1Title 1-1-2
1 10 4 ㄴRE 1Title 1-1-1
1 5 1 ㄴRE 1Title 1-2
2 3 0 3Title
2 13 3 ㄴRE 3Title 1-1
3 2 0 2Title
3 9 2 ㄴRE 2Title 1-3
3 8 2 ㄴRE 2Title 1-2
3 7 2 ㄴRE 2Title 1-1
谢谢你的回答@Sander
我有个问题
insert into tree_table (id, parent_id, name) values
(1, 0, '1Title'),
(2, 0, '2Title'),
(3, 0, '3Title'),
(4, 1, ' ㄴRE 1Title 1-1'),
(5, 1, ' ㄴRE 1Title 1-2'),
(6, 1, ' ㄴRE 1Title 1-3'),
(7, 2, ' ㄴRE 2Title 1-1'),
(8, 2, ' ㄴRE 2Title 1-2'),
(9, 2, ' ㄴRE 2Title 1-3'),
(10, 4, ' ㄴRE 1Title 1-1-1'),
(11, 4, ' ㄴRE 1Title 1-1-2'),
(12, 4, ' ㄴRE 1Title 1-1-3'),
(13, 3, ' ㄴRE 3Title 1-1'),
(14, 1, ' ㄴRE 1Title 1-4'),
(15, 6, ' ㄴRE 3Title 1-3-1');
然后结果
sort_number id parent_id name
-------------------- ----------- ----------- ------------------------------
1 1 0 1Title
1 4 1 ㄴRE 1Title 1-1
1 5 1 ㄴRE 1Title 1-2
1 6 1 ㄴRE 1Title 1-3
1 14 1 ㄴRE 1Title 1-4
1 15 6 ㄴRE 3Title 1-3-1
1 10 4 ㄴRE 1Title 1-1-1
1 11 4 ㄴRE 1Title 1-1-2
1 12 4 ㄴRE 1Title 1-1-3
2 3 0 3Title
2 13 3 ㄴRE 3Title 1-1
3 2 0 2Title
3 7 2 ㄴRE 2Title 1-1
3 8 2 ㄴRE 2Title 1-2
3 9 2 ㄴRE 2Title 1-3
但我想要这样的结果
id parent_id name depth_fullname
1 0 1depth 1 1depth 1
14 1 2depth 1-2 1depth 1 > 2depth 1-4 *Latest
4 1 2depth 1-1 1depth 1 > 2depth 1-1
10 4 3depth 1-1 1depth 1 > 2depth 1-1 > 3depth 1-1
11 4 3depth 1-2 1depth 1 > 2depth 1-1 > 3depth 1-2
12 4 3depth 1-3 1depth 1 > 2depth 1-1 > 3depth 1-3
5 1 2depth 1-2 1depth 1 > 2depth 1-2
6 1 2depth 1-3 1depth 1 > 2depth 1-3
2 0 1depth 2 1depth 2
7 2 2depth 2-1 1depth 2 > 2depth 2-1
8 2 2depth 2-2 1depth 2 > 2depth 2-2
9 2 2depth 2-3 1depth 2 > 2depth 2-3
3 0 1depth 3 1depth 3
13 3 2depth 1-1 1depth 3 > 2depth 1-1
sort_number id parent_id name
-------------------- ----------- ----------- ------------------------------
1 1 0 1Title
1 6 1 ㄴRE 1Title 1-3
1 15 6 ㄴRE 3Title 1-3-1
1 14 1 ㄴRE 1Title 1-4
1 4 1 ㄴRE 1Title 1-1
1 12 4 ㄴRE 1Title 1-1-3
1 11 4 ㄴRE 1Title 1-1-2
1 10 4 ㄴRE 1Title 1-1-1
1 5 1 ㄴRE 1Title 1-2
2 3 0 3Title
2 13 3 ㄴRE 3Title 1-1
3 2 0 2Title
3 9 2 ㄴRE 2Title 1-3
3 8 2 ㄴRE 2Title 1-2
3 7 2 ㄴRE 2Title 1-1
sort_number id parent_id name
-------------------- ----------- ----------- ------------------------------
1 1 0 1Title
1 6 1 ㄴRE 1Title 1-3
1 15 6 ㄴRE 3Title 1-3-1
1 14 1 ㄴRE 1Title 1-4
1 4 1 ㄴRE 1Title 1-1
1 12 4 ㄴRE 1Title 1-1-3
1 11 4 ㄴRE 1Title 1-1-2
1 10 4 ㄴRE 1Title 1-1-1
1 5 1 ㄴRE 1Title 1-2
2 3 0 3Title
2 13 3 ㄴRE 3Title 1-1
3 2 0 2Title
3 9 2 ㄴRE 2Title 1-3
3 8 2 ㄴRE 2Title 1-2
3 7 2 ㄴRE 2Title 1-1
有办法吗?你如何确定哪条评论是最新的?在您的示例中,
id=13
是最新的,因此id
列不能用于跟踪最新的注释。。。如果没有另一种形式的排序(一个递增的数字,一个时间戳,…),我看不到任何方法来确定最后的评论?您不能对不存在的数据进行排序。@Sander再次更正了数据。请检查。因此,id
列指示最后一条注释。图片是否显示了您的“预期结果”?因为这对我来说只是一个id描述的排序…@Sander我想你必须以不同的方式排序是的,回到绘图板上。答案部分不是用来发布新问题的。用“请”将更新合并到原始问题中@桑德更新了调查问卷。请确认!!您替换了问题,而不是将更新合并到问题中。我冒昧地恢复了原来的问题,并自己添加了新的信息。你应该删除这个“答案”。