WITH clause with union all - Oracle 12c

时间:2016-10-17 04:55:39

标签: sql oracle common-table-expression recursive-query

任何人都可以解释以下查询吗?

在查询中使用子句查询别名recursiveBOM? UNION ALL有什么吗?

 WITH recursiveBOM
    (assembly_id, assembly_name, parent_assembly) AS
 (SELECT parent.assembly_id,
         parent.assembly_name,
         parent.parent_assembly
 FROM bill_of_materials parent
 WHERE parent.assembly_id=100
 UNION ALL
 SELECT child.assembly_id,
        child.assembly_name,
        child.parent_assembly
 FROM recursiveBOM parent, bill_of_materials child
 WHERE child.parent_assembly = parent.assembly_id)
 SELECT assembly_id, parent_assembly, assembly_name
 FROM recursiveBOM;

1 个答案:

答案 0 :(得分:1)

WITH recursiveBOM
 AS
 (
 SELECT parent.assembly_id,
         parent.assembly_name,
         parent.parent_assembly
 FROM bill_of_materials parent
 WHERE parent.assembly_id=100
 )
 recursiveBOM2 as
 (
 SELECT child.assembly_id,
        child.assembly_name,
        child.parent_assembly
 FROM   bill_of_materials child
  )
SELECT recursiveBOm.assembly_id, recursiveBOm.parent_assembly, recursiveBOm.assembly_name
 FROM recursiveBOm
 UNION ALL
 SELECT recursiveBOM2.assembly_id, recursiveBOM2.parent_assembly, recursiveBOM2.assembly_name
 FROM recursiveBOm, recursiveBOm2
 WHERE recursiveBOm.assembly_id = recursiveBOM2.parent_assembly
;