Skip site navigation (1) Skip section navigation (2)

Re: Select and order by question

From: "chris smith" <dmagick(at)gmail(dot)com>
To: "Andy Dunlop" <andy(at)infocus(dot)co(dot)za>
Cc: pgsql-php(at)postgresql(dot)org
Subject: Re: Select and order by question
Date: 2006-05-03 12:45:45
Message-ID: 3c1395330605030545r3ff1c607uf2672f963aec6257@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-php
On 5/3/06, Andy Dunlop <andy(at)infocus(dot)co(dot)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/

In response to

pgsql-php by date

Next:From: Sherwin M. HarrisDate: 2006-05-03 14:56:22
Subject: Re: Select and order by question
Previous:From: Volkan YAZICIDate: 2006-05-03 09:19:38
Subject: Re: server dropping connection

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group