pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David W Noon <dwnoon@ntlworld.com>
To: pgsql-sql@postgresql.org
Subject: Re: simple (?) join
Date: Thu, 24 Sep 2009 19:46:29 +0100
Message-ID: <20090924194629.44bc236f@dwnoon.ntlworld.com> (raw)
In-Reply-To: <200909241616.36933.gary.stainburn@ringways.co.uk>
References: <200909241616.36933.gary.stainburn@ringways.co.uk>

On Thu, 24 Sep 2009 16:16:36 +0100, Gary Stainburn wrote about [SQL]
simple (?) join:

>create table orders (
>o_id serial primary key
>...
>);
>
>create table orders_log (
>ol_id serial primary key,
>o_id int4 not null references orders(o_id),
>ol_timestamp timestamp,
>ol_user,
>);
>
>How can I select all from orders and the last (latest) entry from the 
>orders_log?

SELECT * FROM orders
WHERE o_id IN (SELECT o_id FROM orders_log
   WHERE ol_timestamp = (SELECT MAX(ol_timestamp) FROM orders_log));

No joins required.
-- 
Regards,

Dave  [RLU #314465]
=======================================================================
david.w.noon@ntlworld.com (David W Noon)
=======================================================================



view thread (25+ messages)  latest in thread

Message-ID: <20090924194629.44bc236f@dwnoon.ntlworld.com>
Permalink:  ../20090924194629.44bc236f@dwnoon.ntlworld.com/
Also on:    postgresql.org/message-id/20090924194629.44bc236f@dwnoon.ntlworld.com

 · 

reply

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: dwnoon@ntlworld.com
  Subject: Re: simple (?) join
  In-Reply-To: <20090924194629.44bc236f@dwnoon.ntlworld.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