DB2数据库的 递归查询

 

  WITH RPL (PARENT_ID, ID, NAME) AS ( SELECT ROOT.PARENT_ID, ROOT.ID, ROOT.NAME FROM T_ORGANIZATION ROOT WHERE ROOT.PARENT_ID = 10000004 UNION ALL SELECT CHILD.PARENT_ID, CHILD.ID, CHILD.NAME FROM RPL PARENT, T_ORGANIZATION CHILD WHERE PARENT.PARENT_ID = CHILD.ID ) SELECT DISTINCT PARENT_ID, ID, NAME FROM RPL ORDER BY PARENT_ID, ID, NAME

通过一个传入的parent_id(父节点)去取所有的父节点

 

你可能感兴趣的:(数据库,db2)