From: Andy Dunlop <andy@infocus.co.za>
To: Sherwin M. Harris <Sherwin_Harris@byu.edu>
Cc: pgsql-php@postgresql.org
Subject: Re: Select and order by question
Date: Wed, 10 May 2006 09:58:14 +0200
Message-ID: <1147247895.6686.88.camel@localhost.localdomain> (raw)
In-Reply-To: <DBC55FC363C9274CB71DB7600AB0F050047B2E4D@klondike.exch.ad.byu.edu>
References: <DBC55FC363C9274CB71DB7600AB0F050047B2E4D@klondike.exch.ad.byu.edu>
If anyone is interested, here is the code we eventually have used:
<?php
$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 desc limit 1";
$sRes = pg_exec($conn,$sSql);
$sHits = pg_numrows($sRes);
if($sHits!=0){
for($i=0;$i<$sHits;$i++){
$temp_date = explode("-",pg_result($sRes,
$i,"follow_up_date"));
$temp_tstamp = gmmktime(0,0,0,$temp_date[1],
$temp_date[2],
$temp_date[0]);
$array_echo[$temp_tstamp] .=
"<tr><td>".$compName1."</td><td>".$empl1."</td><td>".pg_result($sRes,
$i,"date")."</td><td>".pg_result($sRes,
$i,"description")."</td><td>".pg_result($sRes,
$i,"type")."</td><td>".pg_result($sRes,
$i,"follow_up_action")."</td><td>".pg_result($sRes,
$i,"follow_up_date")."</td><td><a
href='crm_events_edit.php?id=".pg_result($sRes,
$i,"id")."'>Edit</a></td><td><a
href='crm_events_bycomp.php?id=".pg_result($sRes,
$i,"crm_leads_id")."'>Action</a></td></tr>";
}
}
}
?>
<html>
<head>
<title>
CRM - Events
</title>
</head>
<body bgcolor="<?echo $sBodyColor?>">
<?php require("menu.php"); ?>
<table width='100%'>
<tr><td><h2><font color='<? echo $sHeadingColor?>'>Events Requiring
Action</font></h2></td>
<td align='right'><a href='manual/crm_events.html' target='_blank'><img
src='images/help_btn.jpg' border=0></a></td>
</table>
<? asort($array_echo); ?>
<? foreach($array_echo as $ae){
echo $ae;
}
?>
<? echo $prog_echo_end; ?>
</body>
</html>
The limit 1 gets us the last qualifying event on each lead, and this
result is stuck into an array which is sorted by follow_up_date prior to
display.
I probably didn't explain the problem too well! But thanks for the
input.
Andy
On Wed, 2006-05-03 at 08:56 -0600, Sherwin M. Harris wrote:
> Assuming you are using a version of a database that can support
> sub-queries (MySQL 4.1 and up, Postgresql (all reasonably current
> versions, any other robust RDBMS) you can do the query like this:
>
> $sSql = "SELECT * FROM crm_events WHERE crm_leads_id IN (SELECT id
> FROM crm_leads) AND follow_up_action!='' ORDER BY follow_up_date asc
> limit 1"
>
> Not sure what your follow_up_action !=” is suppose to be doing but if
> the rest works for you that should give you what you want.
>
>
>
>
>
>
>
>
> Sherwin Harris
>
> Web Developer
>
> Brigham Young University
>
>
>
>
>
> ______________________________________________________________________
>
> From:pgsql-php-owner@postgresql.org
> [mailto:pgsql-php-owner@postgresql.org] On Behalf Of Andy Dunlop
> Sent: Wednesday, May 03, 2006 3:13 AM
> To: pgsql-php@postgresql.org
> Subject: [PHP] Select and order by question
>
>
>
>
>
> 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?
>
> Any help appreciated
> Thanks
>
> Andy Dunlop
>
> www.infocus.co.za
> +27 21 532 0335 office
> +27 82 770 8749 mobile
>
>
>
>
>
>
Andy Dunlop
www.infocus.co.za
+27 21 532 0335 office
+27 82 770 8749 mobile
Attachments:infocus_cropped.jpg (image/jpeg, 1.9K) downloadview imageimage002.jpg (image/jpeg, 1.3K) downloadview image
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: andy@infocus.co.za, Sherwin_Harris@byu.edu
Subject: Re: Select and order by question
In-Reply-To: <1147247895.6686.88.camel@localhost.localdomain>
References: <DBC55FC363C9274CB71DB7600AB0F050047B2E4D@klondike.exch.ad.byu.edu> <1147247895.6686.88.camel@localhost.localdomain>
* 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