views:

62

answers:

4

Hello. Can anyone tell me how to get and display the biggest values from a database? I have multiple values in my database with the heading "gmd", but how would I get only the first 3 biggest ones to be displayed? How would I do it in this example:

$query  = "SELECT gmd FROM account";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
}

Thanks.

A: 

$query = "SELECT gmd FROM account ORDER BY gmd DESC";

Citizen
A: 

you do like this: Sorry this is the correct query

$query  = "SELECT gmd FROM account ORDER BY gmd DESC limit 3";
Karthik
This doesn't make sense. `max()` is an aggregation across the whole table. It will return only one row, always.
ryeguy
I'm a little disappointed that you can get upvoted for such obviously incorrect answers. What is SO coming to? :(
Joe Philllips
@d03boy, it's crowd wisdom, does not mean it's the best answer but most people accept what they know.
Mark Tomlin
+5  A: 

Use the query to order and limit the results.

SELECT gmd
FROM account
ORDER BY gmd DESC
LIMIT 3

Use your fetch array to display all of the results.

$query  = "SELECT gmd FROM account ORDER BY gmd DESC LIMIT 3";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    echo $row["gmd"];
}
mysql_free_result($result);
Joe Philllips
+1  A: 

For a field containing strings:

SELECT gmd FROM account ORDER BY CHAR_LENGTH( gmd ) DESC LIMIT 3

For a field containing numbers:

SELECT gmd FROM account ORDER BY gmd DESC LIMIT 3
fireeyedboy