Cascading JOINs

from the Artful Common Queries page


Show parents, children and grandchildren including parents without children
SELECT parent.id AS ParentID,
       IFNULL(child.parent_id,') AS ChildParentID,
       IFNULL(child.id,') AS ChildID,
       IFNULL(grandchild.child_id,') AS GrandchildChildID
FROM parent
  LEFT JOIN child ON parent.id=child.parent_id
  LEFT JOIN grandchild ON child.id=grandchild.child_id;

Last updated 22 May 2009




Return to the Artful Common Queries page