Jump to content


MAX value


2 replies to this topic

#1 andrew.craig

    Young Padawan

  • Members
  • Pip
  • 1 posts

Posted 24 May 2005 - 09:24 AM

Hi, can anyone help me with the following:
I am trying to get the MAX or MIN value from a table together with it's associated 'id'.
My SQL statement looks like:

$sql = "SELECT MAX(weight) as maxWeight, id FROM $Log where uid = '$id' GROUP BY id";
$result = mysql_query($sql);

I then do the following:
while (($row = mysql_fetch_row($result)))
{
echo $row[0] . "<BR>";
echo $row[1] . "<BR>";
}

hoping that I would get the maximum value coming out of $row[0] and the id of the row that contains that maximum value out of $row[1]

This doesn't appear to work for me.

What I have done is just a straight forward:
SELECT id, weight FROM $Log where uid = '$id' order by weight DESC LIMIT 0,1
to get the maximum value - is this a stupid way of getting the maximum value ie is there any speed difference in using the MAX() function?

Any help would be appreciated.
Thanks for your time.
Cheers
Andrew

#2 adam123

    Retired P2L Staff

  • Members
  • PipPipPipPip
  • 2,306 posts
  • Location:London, UK
  • Interests:Programming and stuff.

Posted 24 May 2005 - 12:01 PM

Moved to correct forum.

#3 fiv3isaliv3

    Young Padawan

  • Members
  • Pip
  • 258 posts
  • Gender:Male

Posted 27 May 2005 - 03:29 PM

your query is wrong it appears. here is a page from the MySQL manual http://dev.mysql.com/doc/mysql/en/example-...lumn-group.html ... i looked for a page about MIN but i couldn't find it. i'll leave that one to you.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users