Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 5185C9FBF03 for ; Wed, 9 May 2007 15:04:06 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 71518-08 for ; Wed, 9 May 2007 15:04:00 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from exchange.g2switchworks.com (mail.g2switchworks.com [63.87.162.25]) by postgresql.org (Postfix) with ESMTP id 511239FBF4E for ; Wed, 9 May 2007 15:04:02 -0300 (ADT) Received: from 10.10.1.37 ([10.10.1.37]) by exchange.g2switchworks.com ([10.10.1.2]) with Microsoft Exchange Server HTTP-DAV ; Wed, 9 May 2007 18:04:01 +0000 Received: from state.g2switchworks.com by mail.g2switchworks.com; 09 May 2007 13:04:01 -0500 Subject: Re: query to select a linked list From: Scott Marlowe To: Aaron Bono Cc: pgsql-sql@postgresql.org In-Reply-To: References: <20070509125520.GA17497@apartia.fr> Content-Type: text/plain Content-Transfer-Encoding: 7bit Message-Id: <1178733840.7497.59.camel@state.g2switchworks.com> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.6 (1.4.6-2) Date: Wed, 09 May 2007 13:04:01 -0500 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200705/71 X-Sequence-Number: 28351 On Wed, 2007-05-09 at 08:29, Aaron Bono wrote: > On 5/9/07, Louis-David Mitterrand > wrote: > Hi, > > To build a threaded forum application I came up the following > schema: > > forum > ------ > id_forum | integer| not null default > nextval('forum_id_forum_seq'::regclass) > id_parent| integer| > subject | text | not null > message | text | > > Each message a unique id_forum and an id_parent pointing to > the replied > post (empty if first post). > > How can I build an elegant query to select all messages in a > thread? > > Thanks, > > Unlike Oracle, PostgreSQL doesn't have anything like a connect by so > you would need to write your own stored procedure for that (if new > versions of PostgreSQL will have connect by, let me know guys). Take a look at the tablefunc in contrib, that seems to provide connectby. I've only ever used the crosstab functions in there, but I can't see what would have broken in connectby with 8.2 or anything.