Joey Morani
Joey Morani

Reputation: 26581

How to get, and display the biggest values from a database?

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))
{
}

Upvotes: 0

Views: 287

Answers (4)

Karthik
Karthik

Reputation: 3271

you do like this: Sorry this is the correct query

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

Upvotes: -1

Decent Dabbler
Decent Dabbler

Reputation: 22783

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

Upvotes: 1

Joe Phillips
Joe Phillips

Reputation: 51100

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);

Upvotes: 5

Citizen
Citizen

Reputation: 12927

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

Upvotes: 0

Related Questions