对"一个非常难的查询问题(部门上下级的关系)"之解答的
2008-05-01 02:57
666 查看
这是一个CSDN老帖:
http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=170559
我是抱着学习的心态看这个帖子的,下面把握学习结果总结一下。
楼主的问题是这样的:
--------------------------------------------------------------------------------
表A: id name 1 a 2 b 3 c 4 d 5 e 表B(id1,id2都与A表的id关联,是联合主外键): id1 id2 1 2 2 3 2 4 3 5 这是一个部门上下级的关系,前面的是上级,后面的下级,我想得到所有部门的列表,按照级别关系写成完整的字串,结果如下: id full_name 1 a 2 a/b 3 a/b/c 4 a/b/d 5 a/b/c/d 请问怎么写?存储过程或函数都可以,十分感谢!
---------------------------------------------------------
问题的关键是把用LEVEL关键字把层次关系搞搞清楚。
select level from table_B connect by prior id2=id1 start with id1=0;
LEVEL---------- 1 2 3 4 3
SQL> select level from table_B connect by prior id2=id1 start with id1=1;
LEVEL---------- 1 2 3 2
SQL> select level from table_B connect by prior id2=id1 start with id1=2;
LEVEL---------- 1 2 1
SQL> select level from table_B connect by prior id2=id1 start with id1=3;
LEVEL---------- 1
SQL> select level from table_B connect by prior id2=id1 start with id1=4;
LEVEL----------
SQL> select level from table_B connect by prior id2=id1 start with id1=5;
LEVEL----------
可以看出,LEVEL值表示id1领导下人员id2在id1集团所处的层数,被领导者紧跟在by prior后面。
select lpad(id2, level*length(id2), ' ') id, 2 ltrim(sys_connect_by_path(id2,'/'), '/') path 3 from Table_B 4 connect by prior id2=id1 5 start with id1=0 6 /
ID PATH-------------------------------------------------------------------------------- --------------------------------------------------------------------------------1 1 2 1/2 3 1/2/3 5 1/2/3/5 4 1/2/4
下面给出完整解答:
DROP TABLE Table_A;
Table dropped
SQL> create table Table_A (id number(4), name varchar2(20));
Table created
SQL> insert into Table_A values(1, 'a');
1 row inserted
SQL> insert into Table_A values(2, 'b');
1 row inserted
SQL> insert into Table_A values(3, 'c');
1 row inserted
SQL> insert into Table_A values(4, 'd');
1 row inserted
SQL> insert into Table_A values(5, 'e');
1 row inserted
SQL> commit;
Commit complete
SQL> DROP TABLE Table_B;
Table dropped
SQL> create table Table_B (id1 number(4), id2 number(4));
Table created
SQL> insert into table_B values(0,1);
1 row inserted
SQL> insert into Table_B values(1,2);
1 row inserted
SQL> insert into Table_B values(2,3);
1 row inserted
SQL> insert into Table_B values(2,4);
1 row inserted
SQL> insert into Table_B values(3,5);
1 row inserted
SQL> commit;
Commit complete
SQL> SELECT id2, ltrim(sys_connect_by_path(NAME, '/'), '/') path 2 from 3 (SELECT B.*, A.NAME 4 FROM Table_B B, Table_A A 5 WHERE B.id2=A.id) 6 connect by prior id2=id1 7 start with id1 = 0 8 ORDER BY id2 9 /
ID2 PATH----- -------------------------------------------------------------------------------- 1 a 2 a/b 3 a/b/c 4 a/b/d 5 a/b/c/e
http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=170559
我是抱着学习的心态看这个帖子的,下面把握学习结果总结一下。
楼主的问题是这样的:
--------------------------------------------------------------------------------
表A: id name 1 a 2 b 3 c 4 d 5 e 表B(id1,id2都与A表的id关联,是联合主外键): id1 id2 1 2 2 3 2 4 3 5 这是一个部门上下级的关系,前面的是上级,后面的下级,我想得到所有部门的列表,按照级别关系写成完整的字串,结果如下: id full_name 1 a 2 a/b 3 a/b/c 4 a/b/d 5 a/b/c/d 请问怎么写?存储过程或函数都可以,十分感谢!
---------------------------------------------------------
问题的关键是把用LEVEL关键字把层次关系搞搞清楚。
select level from table_B connect by prior id2=id1 start with id1=0;
LEVEL---------- 1 2 3 4 3
SQL> select level from table_B connect by prior id2=id1 start with id1=1;
LEVEL---------- 1 2 3 2
SQL> select level from table_B connect by prior id2=id1 start with id1=2;
LEVEL---------- 1 2 1
SQL> select level from table_B connect by prior id2=id1 start with id1=3;
LEVEL---------- 1
SQL> select level from table_B connect by prior id2=id1 start with id1=4;
LEVEL----------
SQL> select level from table_B connect by prior id2=id1 start with id1=5;
LEVEL----------
可以看出,LEVEL值表示id1领导下人员id2在id1集团所处的层数,被领导者紧跟在by prior后面。
select lpad(id2, level*length(id2), ' ') id, 2 ltrim(sys_connect_by_path(id2,'/'), '/') path 3 from Table_B 4 connect by prior id2=id1 5 start with id1=0 6 /
ID PATH-------------------------------------------------------------------------------- --------------------------------------------------------------------------------1 1 2 1/2 3 1/2/3 5 1/2/3/5 4 1/2/4
下面给出完整解答:
DROP TABLE Table_A;
Table dropped
SQL> create table Table_A (id number(4), name varchar2(20));
Table created
SQL> insert into Table_A values(1, 'a');
1 row inserted
SQL> insert into Table_A values(2, 'b');
1 row inserted
SQL> insert into Table_A values(3, 'c');
1 row inserted
SQL> insert into Table_A values(4, 'd');
1 row inserted
SQL> insert into Table_A values(5, 'e');
1 row inserted
SQL> commit;
Commit complete
SQL> DROP TABLE Table_B;
Table dropped
SQL> create table Table_B (id1 number(4), id2 number(4));
Table created
SQL> insert into table_B values(0,1);
1 row inserted
SQL> insert into Table_B values(1,2);
1 row inserted
SQL> insert into Table_B values(2,3);
1 row inserted
SQL> insert into Table_B values(2,4);
1 row inserted
SQL> insert into Table_B values(3,5);
1 row inserted
SQL> commit;
Commit complete
SQL> SELECT id2, ltrim(sys_connect_by_path(NAME, '/'), '/') path 2 from 3 (SELECT B.*, A.NAME 4 FROM Table_B B, Table_A A 5 WHERE B.id2=A.id) 6 connect by prior id2=id1 7 start with id1 = 0 8 ORDER BY id2 9 /
ID2 PATH----- -------------------------------------------------------------------------------- 1 a 2 a/b 3 a/b/c 4 a/b/d 5 a/b/c/e
相关文章推荐
- 对"一个非常难的查询问题(部门上下级的关系)"之解答的完善
- 对"一个非常难的查询问题(部门上下级的关系)"之解答的完善
- 写了一个查询数据库的程序,不熟的Tomcat6。查询条件为英文时运行正常,为中文时出现乱码。后在tomcap的server.xml中加入useBodyEncodingForURI="true解决了问题
- 遇到的一个"String index out of range" 问题
- 一个"不确定"出现的问题
- 初次将一个C文件 拆开写 遇到的 蛋疼问题"multiple definition of" 错误
- 写了一个上下级关系,根据自己所在部门的编号,找到下级,不包括自己
- 今天遇到一个问题,原来在html的form中如果只有一个type="text"的控件,如果在text中点击回车键,就是自动提交表单!
- 前端datagrid("getSelections")问题&后台SQL查询问题
- hibernate查询的时候出现“列名 "&&"无效”的问题
- #include expects "FILENAME" or 一个很隐蔽的问题
- "java.lang.NoClassDefFoundError"错误——一个关于classpath设置的问题
- 机房重构所遇问题"未能载入文件或程序集“DAL”或它的某一个依赖项。系统找不到指定的文件"的解决的方法集锦
- 关于"Shipping Transactions Form"查询界面中没有显示Additional tab的问题
- JS中关于call()方法的一个问题(转自百度知道,解答非常详细清楚)
- Debug 模式 和 Release 模式下,一个线程在"编译"上引发的问题
- 【转】一个问题引发的"血案" 之 文件的读写原理(fread, fwrite, fflush)
- 一个"如何使用示波器安全测试接市电电路板"的问题
- 郁闷了一天的一个问题"update语句的语法错误"解决啦
- 非常不理解的一个问题。dudu看了请解答下!