Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id F39272E2368 for ; Wed, 14 Nov 2007 08:39:20 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 18655-06 for ; Wed, 14 Nov 2007 08:39:17 -0400 (AST) Received: from smtp20.kymp.net (smtp20.kymp.net [80.248.96.137]) by postgresql.org (Postfix) with ESMTP id 7BC4C2E2002 for ; Wed, 14 Nov 2007 08:39:17 -0400 (AST) Received: from kymifedora ([213.130.241.66]) by smtp20.kymp.net (8.13.3/8.13.3) with ESMTP id lAECdD5J001514; Wed, 14 Nov 2007 14:39:13 +0200 From: Aarni =?iso-8859-1?q?Ruuhim=E4ki?= Reply-To: aarni@kymi.com Organization: KYMI.com To: Richard Huxton Subject: Re: Originally created and last_mod by whom and when ? Date: Wed, 14 Nov 2007 14:39:13 +0200 User-Agent: KMail/1.6.2 References: <200711141306.34996.aarni@kymi.com> <473ADBF7.1060908@archonet.com> In-Reply-To: <473ADBF7.1060908@archonet.com> Cc: pgsql-sql@postgresql.org MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Message-Id: <200711141439.13226.aarni@kymi.com> X-Virus-Scanned: by amavisd-new X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200711/63 X-Sequence-Number: 29591 On Wednesday 14 November 2007 13:28, Richard Huxton wrote: > Aarni Ruuhim=E4ki wrote: > > Hello, > > > > In a web app (Pg 8.2.4 + php) I have product and other tables with fiel= ds > > like > > > > product_created timestamp without time zone > > product_created_user_id integer > > product_last_mod timestamp without time zone > > product_last_mod_user_id integer > > > > The person who last modified an item can obviously be someone else who > > originally created it. > > > > I can get the names and timestamps with two separate queries but how can > > I do a single query to get the names of both ? > > Alias the tables, so you can join to the user-table twice. > > SELECT p.*, u_cre.username as created_by, u_mod.username as modified_by > FROM > products p > LEFT JOIN > app_users u_cre ON p.product_created_user_id =3D u_cre.id > LEFT JOIN > app_users u_mod ON p.product_last_mod_user_id =3D u.mod.id > ; Charming ! Many thanks to you Richard. Aarni =2D-=20