Oracle同一张表内使用In语句查询,多层嵌套如何进行优化?
select * from wms_depot T where T.DEPOTID IN
(select T.DEPOTID from wms_depot T where T.DEPOTID
IN (select T.DEPOTID from wms_depot T where T.DEPOTID = 10002 OR T.DEPOTPARENTID = 10002)
OR T.DEPOTPARENTID IN (select T.DEPOTID from wms_depot T where T.DEPOTID = 10002 OR T.DEPOTPARENTID = 10002))
OR T.DEPOTPARENTID IN (select T.DEPOTID from wms_depot T where T.DEPOTID
IN (select T.DEPOTID from wms_depot T where T.DEPOTID = 10002 OR T.DEPOTPARENTID = 10002)
OR T.DEPOTPARENTID IN (select T.DEPOTID from wms_depot T where T.DEPOTID = 10002 OR T.DEPOTPARENTID = 10002))
------解决方案--------------------可以使用ORACLE中的集合函数进行优化。
------解决方案--------------------看了半天才看明白,根据一个部门id求出它的下面所有的部门,嵌套这么层干嘛啊,汗。。
看看下面的写法是否满足你的需求:
SELECT t.*
FROM wms_depot t
START WITH t.DEPOTID = 10002
CONNECT BY PRIOR t.DEPOTID = t.DEPOTPARENTID