pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: chris smith <dmagick@gmail.com>
To: Andy Dunlop <andy@infocus.co.za>
Cc: pgsql-php@postgresql.org
Subject: Re: Select and order by question
Date: Wed, 3 May 2006 22:45:45 +1000
Message-ID: <3c1395330605030545r3ff1c607uf2672f963aec6257@mail.gmail.com> (raw)
In-Reply-To: <1146647591.7286.284.camel@localhost.localdomain>
References: <1146647591.7286.284.camel@localhost.localdomain>

On 5/3/06, Andy Dunlop <andy@infocus.co.za> wrote:
>
>            Hi - I have the following code:
>
>  //show table of current events needing action
>  $sSql = "SELECT id FROM crm_leads ";
>  $sLeads = pg_exec($conn,$sSql);
>  for($e=0;$e<pg_numrows($sLeads);$e++){
>          $sSql = "SELECT * FROM crm_events WHERE crm_leads_id = ".pg_result($sLeads,$e,"id")." AND follow_up_action!='' ORDER BY follow_up_date asc limit 1";
>          $sRes = pg_exec($conn,$sSql);
>          $sHits = pg_numrows($sRes);
>          if($sHits!=0){
>                  for($i=0;$i<$sHits;$i++){
>                // generate the display here
>          }
>  }
>
>  crm_leads is a parent with many crm_events as it's children.
>  This code gives me the correct set of rows.
>  My problem is that I want only the last row from crm_events (as per limit 1) but I want those rows sorted by follow_up_date. I guess I need to have the whole query in one select statement? But how?

select * from crm_leads l, crm_events e where l.id=e.crm_leads_id and
follow_up_action != '' ORDER BY follow_up_date ASC LIMIT 1;

Make sure crm_events crm_leads_id has an index on id and cram_leads id..

--
Postgresql & php tutorials
http://www.designmagick.com/



view thread (5+ messages)  latest in thread

Message-ID: <3c1395330605030545r3ff1c607uf2672f963aec6257@mail.gmail.com>
Permalink:  ../3c1395330605030545r3ff1c607uf2672f963aec6257@mail.gmail.com/
Also on:    postgresql.org/message-id/3c1395330605030545r3ff1c607uf2672f963aec6257@mail.gmail.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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-php@postgresql.org
  Cc: dmagick@gmail.com, andy@infocus.co.za
  Subject: Re: Select and order by question
  In-Reply-To: <3c1395330605030545r3ff1c607uf2672f963aec6257@mail.gmail.com>
  References: <1146647591.7286.284.camel@localhost.localdomain> <3c1395330605030545r3ff1c607uf2672f963aec6257@mail.gmail.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