sql >> Base de Datos >  >> RDS >> PostgreSQL

Consulta padre/hijo recursiva de PostgreSQL

Para obtener todos los niños del sujeto 1, puede usar

WITH RECURSIVE c AS (
   SELECT 1 AS id
   UNION ALL
   SELECT sa.child_id
   FROM subject_associations AS sa
      JOIN c ON c.id = sa. parent_id
)
SELECT id FROM c;