pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Originally created and last_mod by whom and when ?
3+ messages / 2 participants
[nested] [flat]

* Originally created and last_mod by whom and when ?
@ 2007-11-14 11:06  Aarni Ruuhimäki <aarni@kymi.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Aarni Ruuhimäki @ 2007-11-14 11:06 UTC (permalink / raw)
  To: pgsql-sql

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 ?

product_id | 1
...
product_created_user_id | 1
product_last_mod_user_id | 2

ID 1, created by X / date and time, last_mod by Y / date and time

And a similar query to only one table, users ?

user_id integer
user_forename text
...
user_created timestamp without time zone
user_created_user_id integer
user_last_mod timestamp without time zone
user_last_mod_user_id integer

ID 4, name Z, created by X / date and time, last_mod by Y / date and time

Join, sub select ? I tried some but only managed to get only one name, errors, 
nothing at all or two rows with inner join + union ...

Lotsa thanks for any help,

Aarni
-- 





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Originally created and last_mod by whom and when ?
@ 2007-11-14 11:28  Richard Huxton <dev@archonet.com>
  parent: Aarni Ruuhimäki <aarni@kymi.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Richard Huxton @ 2007-11-14 11:28 UTC (permalink / raw)
  To: aarni@kymi.com; +Cc: pgsql-sql

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
;


-- 
   Richard Huxton
   Archonet Ltd



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Originally created and last_mod by whom and when ?
@ 2007-11-14 12:39  Aarni Ruuhimäki <aarni@kymi.com>
  parent: Richard Huxton <dev@archonet.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Aarni Ruuhimäki @ 2007-11-14 12:39 UTC (permalink / raw)
  To: Richard Huxton <dev@archonet.com>; +Cc: pgsql-sql

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
-- 




^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2007-11-14 12:39 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-11-14 11:06 Originally created and last_mod by whom and when ? Aarni Ruuhimäki <aarni@kymi.com>
2007-11-14 11:28 ` Richard Huxton <dev@archonet.com>
2007-11-14 12:39   ` Aarni Ruuhimäki <aarni@kymi.com>

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