Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id C9D1E63437F for ; Fri, 25 Sep 2009 19:36:28 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 12903-06 for ; Fri, 25 Sep 2009 22:36:12 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-yx0-f192.google.com (mail-yx0-f192.google.com [209.85.210.192]) by mail.postgresql.org (Postfix) with ESMTP id 1A250633D95 for ; Fri, 25 Sep 2009 19:36:17 -0300 (ADT) Received: by yxe30 with SMTP id 30so3894262yxe.29 for ; Fri, 25 Sep 2009 15:36:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type; bh=z0ej1Pq5a4SrEjofpDdrTPiCbY5SpnJTkAOIOqb2LzM=; b=yDZqU13gedqijNCh4qc7PlpSdCjdc6CY8wS4wA3de/oAU136A2LVVZXoNYfHvJOEuc kbiLve+2+CzeMBwEcsWJUWUdo7sJXnCbw0ASZ69uRhQ5OX+lo+5C/vuM2JGeXXWO7oEc OZX2PvINzEMUbG2UBfqxQpu/OdJVaqIFareuc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=OtRYoXTEIDUS24zs6apSvmShighyKCutm7Nr9HrtArOmU4nh7QQ5p4iLQHpugZTqn5 eJKcTkWyGaf7/+cy+zFB+TL+DsJIBCdkXND6TiTmLde/Vkf9R5bFVICx/UtM3vZOq1op IzdN6a3jbEbU+uVjYVJ0FPu7iOKCmYdrTPb4M= MIME-Version: 1.0 Received: by 10.150.116.2 with SMTP id o2mr3443632ybc.34.1253918176737; Fri, 25 Sep 2009 15:36:16 -0700 (PDT) In-Reply-To: <200909241616.36933.gary.stainburn@ringways.co.uk> References: <200909241616.36933.gary.stainburn@ringways.co.uk> Date: Sat, 26 Sep 2009 00:36:16 +0200 Message-ID: Subject: Re: simple (?) join From: Claus Guttesen To: Gary Stainburn Cc: pgsql-sql@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.925 tagged_above=-10 required=5 tests=AWL=0.185, BAYES_05=-1.11 X-Spam-Level: X-Archive-Number: 200909/124 X-Sequence-Number: 33189 > I have two tables > > 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? Maby this will work? Get the unique o_id from the table orders and do a group by in a subselect, and then select the other fields from the same table, ie: select o_od, max(o_field1), max(o_field2) ... from orders where o_id in (select o_id from orders group by o_id) group by o_id order by o_id; This off course assumes that o_id is a serial (and thus that timestamp will always be higher etc.). -- regards Claus When lenity and cruelty play for a kingdom, the gentler gamester is the soonest winner. Shakespeare