Re: Sorting by parent/child relationships

From: "Eric Ridge" <ebr(at)tcdi(dot)com>
To: "Jochem van Dieten" <jochemd(at)oli(dot)tudelft(dot)nl>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Sorting by parent/child relationships
Date: 2001-10-26 20:58:47
Message-ID: D3ADE25911614840BC69C72E3171E4ED0FBD76@tcdiexch.tcdi.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> Some self join would work best I suppose:
>
> select p.*, c.*
> from collection p, collection.c
> where c.parent_id = p.collectionid
> order by p.collectionid ASC, c.collectionid
>
> Depending on your datset you might need to use an outer join instead.
>
> Jochem

Thanks Jochem! This is almost exactly what I needed, and it's much
cleaner than the ugly monster I started with! My biggest problem was
that parent rows had a parent_id of -1, when they needed a parent_id of
collection_id to sort correctly.

eric

Browse pgsql-general by date

  From Date Subject
Next Message mike sears 2001-10-26 21:05:06 Broken pipes
Previous Message Tom Lane 2001-10-26 20:09:50 Re: Optimizing a complex query (long)