Oracle Connect By SQL查询

时间:2014-01-03 09:56:01

标签: sql oracle

在这里,我想让所有的孩子,包括父母使用parentid有什么不对。通过循环用户数据得到Exception连接。 在此先感谢。

查询:

  select *
  from rdt_organization 
  connect by prior parentid = 102;

表格内容:

id  parentid  
102 null
103 102
112 102
104 103
105 null
106 105

预期输出

id  parentid  
102 null
103 102
112 102
104 103

3 个答案:

答案 0 :(得分:3)

您需要使用id和parentid将行连接到PRIOR行,并使用START WITH来决定从哪里开始;

SELECT * 
FROM rdt_organization
START WITH id = 102
CONNECT BY PRIOR id = parentid

An SQLfiddle to test with

答案 1 :(得分:2)

缺少开头

 select *
  from rdt_organization
  start with id = 102  
  connect by prior id = parentid;

答案 2 :(得分:1)

在查询中添加START WITH id = 102并尝试

WITH rdt_organization(ID, parentid) AS
(
SELECT 102, NULL FROM DUAL
UNION ALL
SELECT 103,102 FROM DUAL
UNION ALL
SELECT 112, 102 FROM DUAL
UNION ALL
SELECT 104, 103 FROM DUAL
UNION ALL
SELECT 105, NULL FROM DUAL
UNION ALL
SELECT 106, 105 FROM DUAL
)
SELECT *
FROM   rdt_organization 
START WITH id = 102
CONNECT BY PRIOR ID = parentid;