By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,642 Members | 1,696 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,642 IT Pros & Developers. It's quick & easy.

Syntax for min and max

P: n/a
I wish to assign a variable name to the MIN and MAX values of a field
in a table. The type is int(7).

I am extracting the field with many others with the following code
from a php call.

$ml_collect='SELECT * FROM ml_lopp LEFT JOIN scfmforening ON
(scfmforening.scfmnum=ml_lopp.scfmnum) LEFT JOIN ml_newtidplats ON
(ml_newtidplats.loppnum=ml_lopp.loppnum) ORDER BY date1,
ml_lopp.loppnum';

$ml_upg=mysql_query($ml_collect);

mysql_close();

The field is contained within the table ml_lopp and it is called
loppnum

I have been trying combinations of this to extract the min and max of
the field from the $ml_upg variable which includes all of the data to
be processed.

$lownum=mysql_result($ml_upg,MIN('loppnum'));
$hghnum=mysql_result($ml_upg,MAX('loppnum'));;

Which does not work.

I am very greafull of any help in this matter.

Garry Jones
Sweden

Feb 7 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
$ml_collect='SELECT MIN(ml_lopp.loppnum) as minNum, MAX
(ml_lopp.loppnum) as maxNum FROM ml_lopp LEFT JOIN scfmforening ON
(scfmforening.scfmnum=ml_lopp.scfmnum) LEFT JOIN ml_newtidplats ON
(ml_newtidplats.loppnum=ml_lopp.loppnum) ORDER BY date1,
ml_lopp.loppnum';

$ml_upg=mysql_query($ml_collect);

while ($row = mysql_fetch_object ($ml_upg))
{
$min = $row->minNum;
$max = $row->maxNum;
}
---
http://www.mastervb.net/phpbooks/
http://www.mastervb.net

On Feb 7, 7:59 am, "GarryJones" <mor...@algonet.sewrote:
I wish to assign a variable name to the MIN and MAX values of a field
in a table. The type is int(7).

I am extracting the field with many others with the following code
from a php call.

$ml_collect='SELECT * FROM ml_lopp LEFT JOIN scfmforening ON
(scfmforening.scfmnum=ml_lopp.scfmnum) LEFT JOIN ml_newtidplats ON
(ml_newtidplats.loppnum=ml_lopp.loppnum) ORDER BY date1,
ml_lopp.loppnum';

$ml_upg=mysql_query($ml_collect);

mysql_close();

The field is contained within the table ml_lopp and it is called
loppnum

I have been trying combinations of this to extract the min and max of
the field from the $ml_upg variable which includes all of the data to
be processed.

$lownum=mysql_result($ml_upg,MIN('loppnum'));
$hghnum=mysql_result($ml_upg,MAX('loppnum'));;

Which does not work.

I am very greafull of any help in this matter.

Garry Jones
Sweden

Feb 7 '07 #2

P: n/a
On Tue, 06 Feb 2007 21:49:12 -0800, lorento wrote:
$ml_collect='SELECT MIN(ml_lopp.loppnum) as minNum, MAX
(ml_lopp.loppnum) as maxNum FROM ml_lopp LEFT JOIN scfmforening ON
(scfmforening.scfmnum=ml_lopp.scfmnum) LEFT JOIN ml_newtidplats ON
(ml_newtidplats.loppnum=ml_lopp.loppnum) ORDER BY date1,
ml_lopp.loppnum';

$ml_upg=mysql_query($ml_collect);

while ($row = mysql_fetch_object ($ml_upg))
{
$min = $row->minNum;
$max = $row->maxNum;
}
I'd suggest something along the lines of...

$row = mysql_fetch_object ($ml_upg);
$min = $row->minNum;
$max = $row->maxNum;

while ($row = mysql_fetch_object ($ml_upg))
{
$min = ($row->minNum < $min) ? $row->minNum : $min;
$max = ($row->maxNum $max) ? $row->maxNum : $max;
}

Would work better???

Steve
Feb 7 '07 #3

P: n/a
Steve wrote:
Would work better???
Would work slower.

Assuming that "ml_lopp.loppnum" is indexed, then the database engine ought
to be able to pick out the minimum and maximum values a lot faster than
PHP will be able to. Even without indexes, chances are the database engine
will beat PHP.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/CSS/Javascript/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Feb 9 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.