我在设计系统的时候遇到个问题。
比如当前有一张表,area
表中 area_id作为主键,还有一个字段为parent_id。
其中表中的数据,分别存 省、国家、五大洲
比如 liaoning 的area_id为 1 ,parent_id 为2
china的area_id 为 2 ,parent_id为3
asia的area_id为3
我想,能不能有一条sql语句,通过获得的liaoning的 area_id,将他所有的parent_id获得到呢,包括 china的,asia的,即使asia还有上一层parent_id。
谢谢了。
[b]问题补充:[/b]
MySQL 数据库,麻烦了。
oracle的话,其实就楼上的差不多意思,不知道楼上为什么还嵌套,
这样写就行了
select t.* from area t
start with v_name='liaoning'
connect by area_id = prior parent_id
什么数据库``
ORACLE
select (select t.name from tree t where t.area_id=tr.parent_id) as father,
tr.name
from tree tr
start with tr.area_id=3
connect by PRIOR
tr.parent_id= tr.area_id