From: Aarni Ruuhimäki <aarni@kymi.com>
To: Richard Huxton <dev@archonet.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Originally created and last_mod by whom and when ?
Date: Wed, 14 Nov 2007 14:39:13 +0200
Message-ID: <200711141439.13226.aarni@kymi.com> (raw)
In-Reply-To: <473ADBF7.1060908@archonet.com>
References: <200711141306.34996.aarni@kymi.com>
<473ADBF7.1060908@archonet.com>
On Wednesday 14 November 2007 13:28, Richard Huxton wrote:
> Aarni Ruuhimäki wrote:
> > Hello,
> >
> > In a web app (Pg 8.2.4 + php) I have product and other tables with fields
> > 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 = u_cre.id
> LEFT JOIN
> app_users u_mod ON p.product_last_mod_user_id = u.mod.id
> ;
Charming ! Many thanks to you Richard.
Aarni
--
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: aarni@kymi.com, dev@archonet.com
Subject: Re: Originally created and last_mod by whom and when ?
In-Reply-To: <200711141439.13226.aarni@kymi.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox