数据库表A,字段 节点id,父节点father_id
A
B A
C A
D B
E C
F C
G D
H E
用sql查出G的所有上层节点,结果如下:
id father_id
A
B A
D B
G D
[url]http://huajiang.iteye.com/blog/506604[/url]
oracle
[code="sql"]
select id,father_id from A start by id = 'G'
connect by prior father_id = id
[/code]
[color=indigo]http://zgqhyh.iteye.com/blog/92794
这个很全面,好好研究下 呵呵[/color]
oracle的
其实树形结构不难 你在设计表的时候 一般都是在 留个字段用来保存父节点 这样你无论查询什么 只要稍微想想 都会有结果的 写个存储过程什么的。
http://www.cnblogs.com/hwc_2008/archive/2009/04/10/1433258.html
当然了 它这只是个例子 借鉴下思路。
select * from A a1,A a2
where a1.id='G' and a1.father_id=a2.id;
当使用这种表结构时 没有通用的解决方式
oracle 用connect by
sql server, postgresql, db2 用递归的with语句(这几个数据库的写法还不一样)
之前另外一个人 问过相似的问题。
http://www.iteye.com/problems/33895