oracle:START WITH++++ CONNECT BY PRIOR 关联查询的一个问题
SQL在下面,第三行的条件必须要在最后两行加上去吗?反复试了很多次了,只在where里写会出错,加在后面就OK,到底是为什么啊。自己也分析了下。是不是因为先执行START...
SQL在下面,第三行的条件必须要在最后两行加上去吗?反复试了很多次了,只在where里写会出错,加在后面就OK,到底是为什么啊。自己也分析了下。
是不是因为先执行START WITH++++ CONNECT BY PRIOR 的部分,得到结果之后才去where里判断?如果是那样的话是不是只要是同一个表字段的条件都不能加在where里面了?高手赐教啊……
SELECT ZN.SWJG_DM
FROM T_XT_SWJG_ZN ZN
WHERE ZN.ZN_DM = '1'
START WITH ZN.SWJG_DM = '250000000' and ZN.ZN_DM = '1'
CONNECT BY PRIOR ZN.SWJG_DM = ZN.SJZNJG_DM and ZN.ZN_DM = '1';
那么久了都没有人答- - 展开
是不是因为先执行START WITH++++ CONNECT BY PRIOR 的部分,得到结果之后才去where里判断?如果是那样的话是不是只要是同一个表字段的条件都不能加在where里面了?高手赐教啊……
SELECT ZN.SWJG_DM
FROM T_XT_SWJG_ZN ZN
WHERE ZN.ZN_DM = '1'
START WITH ZN.SWJG_DM = '250000000' and ZN.ZN_DM = '1'
CONNECT BY PRIOR ZN.SWJG_DM = ZN.SJZNJG_DM and ZN.ZN_DM = '1';
那么久了都没有人答- - 展开
1个回答
展开全部
我这里测试,WHERE 那里 加查询条件,一切正常啊。
是不是因为先执行START WITH++++ CONNECT BY PRIOR 的部分,得到结果之后才去where里判断?
经过 查看 查询计划,是这个样子的, 先 START WITH + CONNECT BY PRIOR 之后, 才去做 WHERE 的。 用于测试的 SQL 语句,与查询计划如下:
没有 WHERE 的:
SELECT
LPAD(' ', 2*(LEVEL-1)) || test_val AS test_val
FROM
test_tree
START WITH
test_id = 10
CONNECT BY PRIOR test_id = pid
Rows Execution Plan
------- ---------------------------------------------------
0 SELECT STATEMENT MODE: ALL_ROWS
11 CONNECT BY (WITH FILTERING)
1 TABLE ACCESS (BY INDEX ROWID) OF 'TEST_TREE' (TABLE)
1 INDEX (UNIQUE SCAN) OF 'SYS_C004048' (INDEX (UNIQUE))
10 HASH JOIN
11 CONNECT BY PUMP
80 TABLE ACCESS (FULL) OF 'TEST_TREE' (TABLE)
0 TABLE ACCESS (FULL) OF 'TEST_TREE' (TABLE)
有 WHERE 语句的
SELECT
LPAD(' ', 2*(LEVEL-1)) || test_val AS test_val
FROM
test_tree
WHERE
test_val LIKE '%SQL%'
START WITH
test_id = 10
CONNECT BY PRIOR test_id = pid
Rows Execution Plan
------- ---------------------------------------------------
0 SELECT STATEMENT MODE: ALL_ROWS
4 FILTER
11 CONNECT BY (WITH FILTERING)
1 TABLE ACCESS (BY INDEX ROWID) OF 'TEST_TREE' (TABLE)
1 INDEX (UNIQUE SCAN) OF 'SYS_C004048' (INDEX (UNIQUE))
10 HASH JOIN
11 CONNECT BY PUMP
80 TABLE ACCESS (FULL) OF 'TEST_TREE' (TABLE)
0 TABLE ACCESS (FULL) OF 'TEST_TREE' (TABLE)
对比下来, 有WHERE的。 是最后多一个 FILTER 的处理。
是不是因为先执行START WITH++++ CONNECT BY PRIOR 的部分,得到结果之后才去where里判断?
经过 查看 查询计划,是这个样子的, 先 START WITH + CONNECT BY PRIOR 之后, 才去做 WHERE 的。 用于测试的 SQL 语句,与查询计划如下:
没有 WHERE 的:
SELECT
LPAD(' ', 2*(LEVEL-1)) || test_val AS test_val
FROM
test_tree
START WITH
test_id = 10
CONNECT BY PRIOR test_id = pid
Rows Execution Plan
------- ---------------------------------------------------
0 SELECT STATEMENT MODE: ALL_ROWS
11 CONNECT BY (WITH FILTERING)
1 TABLE ACCESS (BY INDEX ROWID) OF 'TEST_TREE' (TABLE)
1 INDEX (UNIQUE SCAN) OF 'SYS_C004048' (INDEX (UNIQUE))
10 HASH JOIN
11 CONNECT BY PUMP
80 TABLE ACCESS (FULL) OF 'TEST_TREE' (TABLE)
0 TABLE ACCESS (FULL) OF 'TEST_TREE' (TABLE)
有 WHERE 语句的
SELECT
LPAD(' ', 2*(LEVEL-1)) || test_val AS test_val
FROM
test_tree
WHERE
test_val LIKE '%SQL%'
START WITH
test_id = 10
CONNECT BY PRIOR test_id = pid
Rows Execution Plan
------- ---------------------------------------------------
0 SELECT STATEMENT MODE: ALL_ROWS
4 FILTER
11 CONNECT BY (WITH FILTERING)
1 TABLE ACCESS (BY INDEX ROWID) OF 'TEST_TREE' (TABLE)
1 INDEX (UNIQUE SCAN) OF 'SYS_C004048' (INDEX (UNIQUE))
10 HASH JOIN
11 CONNECT BY PUMP
80 TABLE ACCESS (FULL) OF 'TEST_TREE' (TABLE)
0 TABLE ACCESS (FULL) OF 'TEST_TREE' (TABLE)
对比下来, 有WHERE的。 是最后多一个 FILTER 的处理。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询