Jump to content

how to get the maximum value of this column?


ok

Recommended Posts

Hi guys i have these fields below,

 

id |  invited_by  |  tota  l

---------------------------

1 | anna_anna    |  2

2 | iridion_iridion  |  2

3 | solid_solid    |  0

4 | anna_anna    |  2

5 | iridion_iridion  | 3

6 | solid_solid    |  0

 

I want to get the maximum value of the column 'total' which is 3 and under the name of iridion_iridion

 

I have these codes below,

  
$query = "SELECT invited_by, MAX(total) AS total FROM winner_tmp GROUP BY invited_by";
$result = mysql_query($query) or die("Problem with the query: $query on line " . __LINE__ . '<br>' . mysql_error());
$row = mysql_fetch_array($result);

 

But it did not show the right result that i need.

 

Can you show me the right SELECT for this please.

 

Thank you.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

I'm pretty sure you have to use a GROUP BY with the MAX() function. And that won't help you in this case because you need other data from that row any way. Just order the results from highest to lowest and use LIMIT to get the top record

 

SELECT invited_by, total

FROM winner_tmp

ORDER BY total DESC

LIMIT 1

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.