在父子层次结构中排序
Order by in parent child hierarchy
我有以下 table 需要根据链在父子层次结构中排序...我不知道如何根据父 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
我想要 table 根据父子层次结构中的链按以下方式排序..
Strand
3.1
3.1.1
3.1.1.1
3.1.1.2
3.1.2
3.2
3.2.1
3.3
几个小时以来一直坚持...知道如何进行吗?
您可以使用 rpad 进行排序(添加一些字符以构建相同长度的字符串)
select * from your_table
order by rpad(trim(strand), 64, 'Z')
使用这个
SELECT * FROM your_table
ORDER BY RPAD(trim(strand),50,'.0');
这样,您正在将 3.1
更改为 3.1.0.0.0.
,因此排序是正确的。
将 10
更改为超过 max(length(strand))
我有以下 table 需要根据链在父子层次结构中排序...我不知道如何根据父 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
我想要 table 根据父子层次结构中的链按以下方式排序..
Strand
3.1
3.1.1
3.1.1.1
3.1.1.2
3.1.2
3.2
3.2.1
3.3
几个小时以来一直坚持...知道如何进行吗?
您可以使用 rpad 进行排序(添加一些字符以构建相同长度的字符串)
select * from your_table
order by rpad(trim(strand), 64, 'Z')
使用这个
SELECT * FROM your_table
ORDER BY RPAD(trim(strand),50,'.0');
这样,您正在将 3.1
更改为 3.1.0.0.0.
,因此排序是正确的。
将 10
更改为超过 max(length(strand))