从SQL表树形 [英] Treeview from sql table
本文介绍了从SQL表树形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像below.I SQL表已经显示它在树视图
ID PARENTID名
1空后市
2 1阴
3 1阴雨
4 1晴天
5 2是
6 3风
7 4湿度
8 6强
9 6弱
10 7高
11 8无
12月9日是
13 10否
14 15是
15 7正常
请帮助我,如果任何人知道......它的紧迫......
我要为
输出-Outlook
- 阴
- 是
- 阴雨
- 风
- 强大
- 没有
- 弱
- 是
-Sunny
- 湿度
-高
-没有
-正常
-是
只有一个根节点展望here.then来自子节点和子子节点之类的。
解决方案
带Q AS
(
选择 *
FROM mytable的
WHERE PARENTID IS NULL - 这个条件定义在链中的最终祖先,将其更改为适当的
UNION ALL
选择米*。
FROM mytable的米
JOIN q
ON m.parentID = q.ID
)
选择 *
从q
I have sql table like below.I have to show it in tree view
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
please help me if any one knows....its urgent....
I want output as
-outlook
- overcast
- yes
- rainy
- wind
- strong
- no
- weak
- yes
-sunny
- humidity
-high
-no
-normal
-yes
There is only one root node 'outlook' here.then comes child nodes and sub-child nodes like that.
解决方案
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
这篇关于从SQL表树形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文