我有如下所示的 sql 表。我必须在树视图中显示它
id parentid name
1 NULL outlook
2 1 overcast
3 1 rainy
4 1 sunny
5 2 yes
6 3 wind
7 4 humidity
8 6 strong
9 6 weak
10 7 high
11 8 no
12 9 yes
13 10 no
14 15 yes
15 7 normal
我想要输出为
-outlook
- overcast
- yes
- rainy
- wind
- strong
- no
- weak
- yes
-sunny
- humidity
-high
-no
-normal
-yes
这里只有一个根节点“outlook”。然后是子节点和子子节点。
WITH q AS
(
SELECT *
FROM mytable
WHERE ParentID IS NULL -- this condition defines the ultimate ancestors in your chain, change it as appropriate
UNION ALL
SELECT m.*
FROM mytable m
JOIN q
ON m.parentID = q.ID
)
SELECT *
FROM q
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)