Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 683B99FBDA9 for ; Wed, 9 May 2007 21:15:15 -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 35886-03 for ; Wed, 9 May 2007 21:15:06 -0300 (ADT) X-Greylist: delayed 00:25:25.486838 by SQLgrey-1.7.4 Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 239DB9FBC86 for ; Wed, 9 May 2007 21:15:11 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from anumail7.anu.edu.au (anumail7.anu.edu.au [130.56.64.133]) by svr4.postgresql.org (Postfix) with ESMTP id 58DC95AF031 for ; Wed, 9 May 2007 23:49:43 +0000 (GMT) Received: from dcsmail (dcsmail.anu.edu.au [150.203.164.16]) by anumail7.anu.edu.au (8.13.8/8.13.8) with ESMTP id l49NnYIe014260 for ; Thu, 10 May 2007 09:49:34 +1000 (EST) (envelope-from bob@cs.anu.edu.au) Received: from littleblue.anu.edu.au ([150.203.160.52] ident=bob) by dcsmail with esmtp (Exim 4.50) id 1HlvuW-0003Gh-M7 for pgsql-sql@postgresql.org; Thu, 10 May 2007 09:49:34 +1000 Message-ID: <46425E0C.8080804@cs.anu.edu.au> Date: Thu, 10 May 2007 09:49:32 +1000 From: Robert Edwards User-Agent: Thunderbird 1.5.0.9 (X11/20070104) MIME-Version: 1.0 To: pgsql-sql@postgresql.org References: <20070509125520.GA17497@apartia.fr> In-Reply-To: <20070509125520.GA17497@apartia.fr> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-SA-Exim-Connect-IP: 150.203.160.52 X-SA-Exim-Mail-From: bob@cs.anu.edu.au Subject: Re: query to select a linked list X-SA-Exim-Version: 4.2 (built Thu, 03 Mar 2005 10:44:12 +0100) X-SA-Exim-Scanned: Yes (on dcsmail) X-PMX-Version: 5.3.1.294258, Antispam-Engine: 2.5.1.298604, Antispam-Data: 2007.5.9.162933 internal X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200705/73 X-Sequence-Number: 28353 Hi Louis-David, I also have written a forum application using PostgreSQL. My schema has a "threadid" for each posting, which is actually also the "messageid" of the first posting in the thread, but that is irrelevant. I can then just select all messages belonging to that thread. The actual hierarchy of messages (which posting is in response to which) is dealt with by a "parentid", identifying the messageid of the post being responded to. Sorting that out is done by the middleware (PHP in this case) - the SQL query simply returns all messages in the thread in a single query. Because our database is somewhat busy, I have opted to keep the queries to the database simple and let the middleware sort out the heirarchical structure (which it is quite good at). I hope this helps. Bob Edwards. 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, > > ---------------------------(end of broadcast)--------------------------- > TIP 1: if posting/reading through Usenet, please send an appropriate > subscribe-nomail command to majordomo@postgresql.org so that your > message can get through to the mailing list cleanly