oracle中的sql用递归查询树格式数据
1.查询当前节点的子类
WITH cte_child ( GRID_ID, GRID_NAME, PARENT_GRID_CODE ) AS ( --起始条件 SELECT GRID_ID, GRID_NAME, PARENT_GRID_CODE FROM T_SG_GRID WHERE GRID_ID = '2' --列出父节点查询条件 UNION ALL --递归条件 SELECT A .GRID_ID, A .GRID_NAME, A .PARENT_GRID_CODE FROM T_SG_GRID A INNER JOIN cte_child b ON ( A .PARENT_GRID_CODE = b.GRID_ID ) ) SELECT * FROM cte_child;
2.查询当前节点的父类
WITH cte_child ( GRID_ID, GRID_NAME, PARENT_GRID_CODE ) AS ( --起始条件 SELECT GRID_ID, GRID_NAME, PARENT_GRID_CODE FROM T_SG_GRID WHERE GRID_ID = '2' --列出父节点查询条件 UNION ALL --递归条件 SELECT A .GRID_ID, A .GRID_NAME, A .PARENT_GRID_CODE FROM T_SG_GRID A INNER JOIN cte_child b ON ( A .GRID_ID = b.PARENT_GRID_CODE ) ) SELECT * FROM cte_child;
也就是把查询条件换了一个位置而已
世间种种的诱惑,不惊不扰我清梦