Recursive CTE - Get descendants (many-to-many relationship)

后端 未结 1 1486
醉酒成梦
醉酒成梦 2021-01-24 12:38

What I have:

Given a tree (or more like a directed graph) that describes how a system is composed by its generic parts. For now let this system be e.g. the human body

相关标签:
1条回答
  • 2021-01-24 13:00

    Problem solved, here is the query I needed, I hope it once helps someone else too...

    WITH RECURSIVE graph AS (
      SELECT
        p.id AS subpart,
        pp.cont AS part
      FROM part p JOIN partpart pp
      ON p.id = pp.part
      WHERE pp.cont = 3
      UNION ALL
      SELECT
        part.id,
        partpart.cont
      FROM (part JOIN partpart
      ON part.id = partpart.part
      ), graph WHERE partpart.cont = graph.subpart
    )
    SELECT part, subpart, FROM graph
    
    0 讨论(0)
提交回复
热议问题