gpt4 book ai didi

mysql - 在父子层次结构中排序

转载 作者:行者123 更新时间:2023-11-29 19:13:15 25 4
gpt4 key购买 nike

我有下表,需要根据链在父子层次结构中进行排序...我不知道如何根据父 ID 对其进行排序。

sid    parent_id    snum  strand         Sname
1 0 3 3.1 civics
2 0 3 3.2 geography
3 0 3 3.3 history
4 1 3 3.1.1 civic and insti
5 1 3 3.1.2 civic and poli
6 2 3 3.2.1 geo poli
7 4 3 3.1.1.1 CIVI
8 4 3 3.1.1.2 CIVVISSSDS

我希望按照以下方式根据父子层次结构中的链对表进行排序..

Strand 
3.1
3.1.1
3.1.1.1
3.1.1.2
3.1.2
3.2
3.2.1
3.3

几个小时以来一直卡在这个问题上......知道如何继续吗?

最佳答案

使用这个

SELECT * FROM your_table
ORDER BY RPAD(trim(strand),50,'.0');

http://rextester.com/NIK55090

这样,您就可以将 3.1 更改为 3.1.0.0.0.,因此排序是正确的。将 10 更改为大于 max(length(strand))

关于mysql - 在父子层次结构中排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42906340/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com