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.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.