Select and order by question
am 03.05.2006 11:13:10 von Andy Dunlop
--=-sOG5NXamD3uWs9jff1xs
Content-Type: multipart/alternative; boundary="=-Zh7ZMpfLl0g840bQSXCt"
--=-Zh7ZMpfLl0g840bQSXCt
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
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
$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
--=-Zh7ZMpfLl0g840bQSXCt
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit
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
|
--=-Zh7ZMpfLl0g840bQSXCt--
--=-sOG5NXamD3uWs9jff1xs
Content-ID: <1146646853.7286.270.camel@localhost.localdomain>
Content-Disposition: attachment; filename=infocus_cropped.jpg
Content-Type: image/jpeg; name=infocus_cropped.jpg
Content-Transfer-Encoding: base64
/9j/4AAQSkZJRgABAQEAlQCVAAD/4QAWRXhpZgAATU0AKgAAAAgAAAAAAAD/ /gAXQ3JlYXRlZCB3
aXRoIFRoZSBHSU1Q/9sAQwAFAwQEBAMFBAQEBQUFBgcMCAcHBwcPCwsJDBEP EhIRDxERExYcFxMU
GhURERghGBodHR8fHxMXIiQiHiQcHh8e/9sAQwEFBQUHBgcOCAgOHhQRFB4e Hh4eHh4eHh4eHh4e
Hh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4e/8AAEQgAJABz AwEiAAIRAQMRAf/E
AB0AAAIBBQEBAAAAAAAAAAAAAAAIBwEDBAUGAgn/xAA4EAABAwQBAwEGAgcJ AAAAAAABAgMEAAUG
EQcSITFBCBMiUWFxFGIyRFOBkaHBFRYkJTlSZYLw/8QAGgEBAAMBAQEAAAAA AAAAAAAAAAECBAYD
B//EACARAAMAAQUBAAMAAAAAAAAAAAABAgMEBREhMVESMkL/2gAMAwEAAhED EQA/AGZumfR4HJVv
whVlubjs1oOJmoaJjt9lnSleAfg158qFdeV638J+nY1AGZck5dbfbCxfjuHc UIx64QQ9JjmO2VLU
W3z2X09Q7to8H0+tYntbcoZzx/mGDW7DZTCBeHHUyI7kdCxIKVspSjqUCUg9 ZGx370AxYWCe3f7V
j3KdGt8CROmOJZjxmlPOrV4ShIKlH9wBNQJl2Jc6qsEvJ2uTWodwjx1SU25i IkMICQVe78Hfy2dm
uRh5ryRyv7ON8ukK/W6zybZDmtXttUJK/wAc0lhfUlPb4CRvuPFCV6d0faiw V6WtEK3XqXDQ97lU
1uMr3QVvQ76+lWLz7QcF6SpqxxA+2lXT7xXrSbWa5ZOvjWJakXkJsKJDr34N DQSpKuxKlKA2QT6b
rLwhjIL6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hSF9L6Fa7p6T3J9K53CeR8j5fx3NGYmNiHj7cZ1i2TVJWFyuxA7E9zrRq V5nHWBTJv42VhWNv
yurr985a2VL38+op3XRR4rEZltiO0hlpsAIQ2npSkAa0B6Dx2FbjmhSfY15M w/B+P52JZjd2LBdI
k9xZROV7sLSddwT9dj91afO80t+ce2Tx1crQy+u3sFtiK+tHSmQA47tafyhR I3+XdNldsFwu7zvx
91xKwz5f7eRbmXHD/wBindZEjE8ZkXOFc5GP2l6dAQEQpLkNtTsYDZ02ojaR snxryaAWfLsm4ezP
kW+WDl3C28Ynw3Pds3B5ZbU+ATpSnW9b2AkjZPpUfX9mwYXm2PxeAs7u94ky ZmpFvjSVPx0oJGyr
XbxvzTuX7GMdyBKU36xWu7JQOlImw23gkfIdYNW7DiWMY+oqsOPWm1KIIJhw 22d9/wAoFAbKEHVQ
2VSh0yC2kuhPgK13/nRWQQfQgD7UUBWiiigCgjYooqGC2P60KQhf6SQr7iii rUuist9HhISlXSlI
A+gq8AN+KKKrfSXB6MrRRRUlQooooAooooAooooD/9k=
--=-sOG5NXamD3uWs9jff1xs--
Re: Select and order by question
am 03.05.2006 14:45:45 von Chris Smith
On 5/3/06, Andy Dunlop wrote:
>
> Hi - I have the following code:
>
> //show table of current events needing action
> $sSql =3D "SELECT id FROM crm_leads ";
> $sLeads =3D pg_exec($conn,$sSql);
> for($e=3D0;$e
> $sSql =3D "SELECT * FROM crm_events WHERE crm_leads_id =3D ".pg_=
result($sLeads,$e,"id")." AND follow_up_action!=3D'' ORDER BY follow_up_dat=
e asc limit 1";
> $sRes =3D pg_exec($conn,$sSql);
> $sHits =3D pg_numrows($sRes);
> if($sHits!=3D0){
> for($i=3D0;$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 limi=
t 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=3De.crm_leads_id and
follow_up_action !=3D '' 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/
---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster
Re: Select and order by question
am 03.05.2006 16:56:22 von Sherwin_Harris
This is a multi-part message in MIME format.
------_=_NextPart_001_01C66EC1.BEEE2A7F
Content-Type: multipart/alternative;
boundary="----_=_NextPart_002_01C66EC1.BEEE2A7F"
------_=_NextPart_002_01C66EC1.BEEE2A7F
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
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 =3D "SELECT * FROM crm_events WHERE crm_leads_id IN (SELECT id =
FROM
crm_leads) AND follow_up_action!=3D'' ORDER BY follow_up_date asc limit =
1"
Not sure what your follow_up_action !=3D" is suppose to be doing but if
the rest works for you that should give you what you want.
=20
=20
Sherwin Harris
Web Developer
Brigham Young University
=20
________________________________
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
=20
Hi - I have the following code:
//show table of current events needing action
$sSql =3D "SELECT id FROM crm_leads ";
$sLeads =3D pg_exec($conn,$sSql);
for($e=3D0;$e
$sSql =3D "SELECT * FROM crm_events WHERE crm_leads_id =3D
".pg_result($sLeads,$e,"id")." AND follow_up_action!=3D'' ORDER BY
follow_up_date asc limit 1";
$sRes =3D pg_exec($conn,$sSql);
$sHits =3D pg_numrows($sRes);
if($sHits!=3D0){
for($i=3D0;$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?=20
Any help appreciated
Thanks
Andy Dunlop
=20
www.infocus.co.za
+27 21 532 0335 office
+27 82 770 8749 mobile=20
=20
------_=_NextPart_002_01C66EC1.BEEE2A7F
Content-Type: text/html;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
charset=3Dus-ascii">
style=3D'font-size:
10.0pt;font-family:Arial;color:navy'>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:
style=3D'font-size:
12.0pt'>$sSql =3D "SELECT * FROM crm_events WHERE crm_leads_id IN =
(SELECT id
FROM crm_leads) AND follow_up_action!=3D'' ORDER BY follow_up_date asc =
limit
1"
style=3D'font-size:
12.0pt'>Not sure what your follow_up_action !=3D” is suppose to be =
doing but if
the rest works for you that should give you what you =
want.
style=3D'font-size:
10.0pt;font-family:Arial;color:navy'>
1.0pt;padding:0in 0in 1.0pt 0in'>
color=3Dnavy
face=3DArial>
style=3D'font-size:10.0pt;font-family:Arial;color:navy'>
nt>
style=3D'font-size:10.0pt;font-family:Arial;color:#0000A0'>S herwin =
Harris
style=3D'font-size:10.0pt;font-family:Arial;color:#0000A0'>W eb =
Developer
style=3D'font-size:10.0pt;font-family:Arial;color:#0000A0'>B righam Young
University
Roman">
style=3D'font-size:12.0pt;color:navy'>
size=3D3
face=3D"Times New Roman">
style=3D'font-size:10.0pt;
font-family:Tahoma;font-weight:bold'>From:
size=3D2
face=3DTahoma>
pgsql-php-owner@postgresql.org [mailto:pgsql-php-owner@postgresql.org] =
style=3D'font-weight:bold'>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
style=3D'font-size:
12.0pt'>
style=3D'font-size:
12.0pt'>Hi - I have the following code:
//show table of current events needing action
$sSql =3D "SELECT id FROM crm_leads ";
$sLeads =3D pg_exec($conn,$sSql);
for($e=3D0;$e<pg_numrows($sLeads);$e++){
$sSql =3D "SELECT * FROM
crm_events WHERE crm_leads_id =3D
".pg_result($sLeads,$e,"id")." AND =
follow_up_action!=3D''
ORDER BY follow_up_date asc limit 1";
$sRes =3D =
pg_exec($conn,$sSql);
$sHits =3D =
pg_numrows($sRes);
if($sHits!=3D0){
&=
nbsp;
for($i=3D0;$i<$sHits;$i++){
&=
nbsp;
// 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
width=3D"100%"
style=3D'width:100.0%'>
style=3D'font-size:12.0pt'>Andy Dunlop
align=3Dbottom border=3D0>
www.infocus.co.za
+27 21 532 0335 office
+27 82 770 8749 mobile
|
style=3D'font-size:
12.0pt'>
------_=_NextPart_002_01C66EC1.BEEE2A7F--
------_=_NextPart_001_01C66EC1.BEEE2A7F
Content-Type: image/jpeg;
name="image002.jpg"
Content-Transfer-Encoding: base64
Content-ID:
Content-Description: image002.jpg
Content-Location: image002.jpg
/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAoHBwgHBgoICAgLCgoLDhgQDg0N Dh0VFhEYIx8lJCIf
IiEmKzcvJik0KSEiMEExNDk7Pj4+JS5ESUM8SDc9Pjv/2wBDAQoLCw4NDhwQ EBw7KCIoOzs7Ozs7
Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozv/ wAARCAAZAFADASIA
AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAA AgEDAwIEAwUFBAQA
AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcY GRolJicoKSo0NTY3
ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKT lJWW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3kOlWNtf3F/Ba RR3V0AJ5lXDSY6ZN
Lp+lWOk25t9PtY7aEuXKRjA3HqauUUAZLeGdENhJYHS7Y2s0vnSQ7PlZ853E euawtb8C219M8sUU
bo/LROMj8K7I9KSpaTRtRrzotuD3OL0fwRBp77oLGC1Y8M6r81dDd+HNI1G0 S21DT7e7jj+750Yb
BrS/ip9KMUgq1ZVNyhpuiaZo8Bg02wt7SNvvLFGF3fX1qong/wAOR3b3aaJZ LPICGkEIyc9a2qKs
xMqTwxocumRaZLpVq9nDny4GiBVM+npT9K8PaPom7+y9MtrPf94xRgE/jWlR QB//2Q==
------_=_NextPart_001_01C66EC1.BEEE2A7F--
Re: Select and order by question
am 03.05.2006 18:12:43 von operationsengineer1
off topic, but still relevant to php sql. i'll share
something that has made my life much easier wrt sql
statements - heredocs.
the format looks like this:
$sSql =3D <<<_ESQL
SELECT * FROM crm_events=20
WHERE crm_leads_id
IN (SELECT id FROM crm_leads)=20
AND follow_up_action!=3D''=20
ORDER BY follow_up_date asc limit 1
_ESQL
the benefits are that it is much more readable, you
don't have to worry about escaping the usual suspect
characters and you can easily copy and paste this code
into a sql tool to test the results of the sql. you
can also copy and paste from the sql tool back into
your code. this helps dramatically when you have
20-30 lines of sql with quotes throughout. ;-)
you can look up heredoc in the php for more
information.
good luck.
--- "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:
>=20
> $sSql =3D "SELECT * FROM crm_events WHERE crm_leads_id
> IN (SELECT id FROM
> crm_leads) AND follow_up_action!=3D'' ORDER BY
> follow_up_date asc limit 1"
>=20
> Not sure what your follow_up_action !=3D" is suppose
> to be doing but if
> the rest works for you that should give you what you
> want.
>=20
> =20
>=20
> =20
>=20
> Sherwin Harris
>=20
> Web Developer
>=20
> Brigham Young University
>=20
> =20
>=20
> ________________________________
>=20
> 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
>=20
> =20
>=20
> Hi - I have the following code:
>=20
> //show table of current events needing action
> $sSql =3D "SELECT id FROM crm_leads ";
> $sLeads =3D pg_exec($conn,$sSql);
> for($e=3D0;$e
> $sSql =3D "SELECT * FROM crm_events WHERE
> crm_leads_id =3D
> ".pg_result($sLeads,$e,"id")." AND
> follow_up_action!=3D'' ORDER BY
> follow_up_date asc limit 1";
> $sRes =3D pg_exec($conn,$sSql);
> $sHits =3D pg_numrows($sRes);
> if($sHits!=3D0){
> for($i=3D0;$i<$sHits;$i++){
> // generate the display here
> }
> }
>=20
> 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?=20
>=20
> Any help appreciated
> Thanks
>=20
> Andy Dunlop
> =20
> www.infocus.co.za
> +27 21 532 0335 office
> +27 82 770 8749 mobile=20
>=20
> =20
>=20
>=20
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around=20
http://mail.yahoo.com=20
---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?
http://www.postgresql.org/docs/faq
Re: Select and order by question
am 03.05.2006 21:25:24 von John DeSoi
On May 3, 2006, at 12:12 PM,
wrote:
> off topic, but still relevant to php sql. i'll share
> something that has made my life much easier wrt sql
> statements - heredocs.
>
> the format looks like this:
>
> $sSql = <<<_ESQL
>
> 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
>
> _ESQL
A little shameless promotion: pgEdit will even color the heredoc
string for SQL if you start your heredoc string with <<
for <<
I'll have a short article out soon that shows how you can pretty much
remove all SQL from your PHP code using a simple PHP class and
PostgreSQL functions.
John DeSoi, Ph.D.
http://pgedit.com/
Power Tools for PostgreSQL
---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?
http://www.postgresql.org/docs/faq
Re: Select and order by question
am 10.05.2006 09:58:14 von Andy Dunlop
--=-h4COFrQejVYZef99DnpJ
Content-Type: multipart/alternative; boundary="=-Kprzzbv1kpE5F3Fsq9dx"
--=-Kprzzbv1kpE5F3Fsq9dx
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
If anyone is interested, here is the code we eventually have used:
$sSql =3D "select id from crm_leads ";
$sLeads =3D pg_exec($conn,$sSql);=20
for($e=3D0;$e
$sSql =3D "SELECT * FROM crm_events where crm_leads_id =3D
".pg_result($sLeads,$e,"id")." and follow_up_action!=3D'' ORDER BY
follow_up_date desc limit 1";
$sRes =3D pg_exec($conn,$sSql);
$sHits =3D pg_numrows($sRes);
=20
if($sHits!=3D0){
=20
for($i=3D0;$i<$sHits;$i++){
=20
$temp_date =3D explode("-",pg_result($sRes,
$i,"follow_up_date"));
$temp_tstamp =3D gmmktime(0,0,0,$temp_date[1],
$temp_date[2],
$temp_date[0]); =20
$array_echo[$temp_tstamp] .=3D
"
".$compName1." | ".$empl1." | ".pg_result($sRes,
$i,"date")." | ".pg_result($sRes,
$i,"description")." | ".pg_result($sRes,
$i,"type")." | ".pg_result($sRes,
$i,"follow_up_action")." | ".pg_result($sRes,
$i,"follow_up_date")." |
href=3D'crm_events_edit.php?id=3D".pg_result($sRes,
$i,"id")."'>Edit |
href=3D'crm_events_bycomp.php?id=3D".pg_result($sRes,
$i,"crm_leads_id")."'>Action |
";
}
}
}
?>
CRM - Events
">
asort($array_echo); ?>
foreach($array_echo as $ae){
echo $ae;
}
?>
echo $prog_echo_end; ?>
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:
>=20
> $sSql =3D "SELECT * FROM crm_events WHERE crm_leads_id IN (SELECT id
> FROM crm_leads) AND follow_up_action!=3D'' ORDER BY follow_up_date asc
> limit 1"
>=20
> Not sure what your follow_up_action !=â is suppose to be doin=
g but if
> the rest works for you that should give you what you want.
>=20
> =20
>=20
>=20
> =20
>=20
>=20
>=20
> Sherwin Harris
>=20
> Web Developer
>=20
> Brigham Young University
>=20
> =20
>=20
>=20
> =20
> ____________________________________________________________ __________
>=20
> 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
>=20
>=20
>=20
> =20
>=20
> Hi - I have the following code:
>=20
> //show table of current events needing action
> $sSql =3D "SELECT id FROM crm_leads ";
> $sLeads =3D pg_exec($conn,$sSql);
> for($e=3D0;$e
> $sSql =3D "SELECT * FROM crm_events WHERE crm_leads_id =3D
> ".pg_result($sLeads,$e,"id")." AND follow_up_action!=3D'' ORDER BY
> follow_up_date asc limit 1";
> $sRes =3D pg_exec($conn,$sSql);
> $sHits =3D pg_numrows($sRes);
> if($sHits!=3D0){
> for($i=3D0;$i<$sHits;$i++){
> // generate the display here
> }
> }
>=20
> 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?=20
>=20
> Any help appreciated
> Thanks
>=20
> Andy Dunlop
>=20
> www.infocus.co.za
> +27 21 532 0335 office
> +27 82 770 8749 mobile=20
>=20
>=20
>=20
> =20
>=20
>=20
Andy Dunlop
www.infocus.co.za
+27 21 532 0335 office
+27 82 770 8749 mobile
--=-Kprzzbv1kpE5F3Fsq9dx
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit
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&
gt;</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
|
--=-Kprzzbv1kpE5F3Fsq9dx--
--=-h4COFrQejVYZef99DnpJ
Content-ID: <1147246953.6686.74.camel@localhost.localdomain>
Content-Disposition: attachment; filename=infocus_cropped.jpg
Content-Type: image/jpeg; name=infocus_cropped.jpg
Content-Transfer-Encoding: base64
/9j/4AAQSkZJRgABAQEAlQCVAAD/4QAWRXhpZgAATU0AKgAAAAgAAAAAAAD/ /gAXQ3JlYXRlZCB3
aXRoIFRoZSBHSU1Q/9sAQwAFAwQEBAMFBAQEBQUFBgcMCAcHBwcPCwsJDBEP EhIRDxERExYcFxMU
GhURERghGBodH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kW+WDl3C28Ynw3Pds3B5ZbU+ATpSnW9b2AkjZPpUfX9mwYXm2PxeAs7u94ky ZmpFvjSVPx0oJGyr
XbxvzTuX7GMdyBKU36xWu7JQOlImw23gkfIdYNW7DiWMY+oqsOPWm1KIIJhw 22d9/wAoFAbKEHVQ
2VSh0yC2kuhPgK13/nRWQQfQgD7UUBWiiigCgjYooqGC2P60KQhf6SQr7iii rUuist9HhISlXSlI
A+gq8AN+KKKrfSXB6MrRRRUlQooooAooooAooooD/9k=
--=-h4COFrQejVYZef99DnpJ
Content-Type: image/jpeg; name="image002.jpg"
Content-ID:
Content-Description: image002.jpg
Content-Location: image002.jpg
Content-Transfer-Encoding: base64
/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAoHBwgHBgoICAgLCgoLDhgQDg0N Dh0VFhEYIx8lJCIf
IiEmKzcvJik0KSEiMEExNDk7Pj4+JS5ESUM8SDc9Pjv/2wBDAQoLCw4NDhwQ EBw7KCIoOzs7Ozs7
Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozv/ wAARCAAZAFADASIA
AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAA AgEDAwIEAwUFBAQA
AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcY GRolJicoKSo0NTY3
ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKT lJWWl5iZmqKjpKWm
p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP0 9fb3+Pn6/8QAHwEA
AwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQA AQJ3AAECAxEEBSEx
BhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKj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