Reputation: 11
I have this table :
fistname | lastname | english
---------------------------------------------
bob | wilson | 77
jess | farr | 47
And this is my code in php :
$con = mysql_connect('localhost','root','');
mysql_select_db('education');
$highest= mysql_query("SELECT firstname, lastname, max(english)FROM students");
$row = mysql_fetch_row($highest);
echo 'English Highscore:' ;
echo "\r\n" ;
echo $row[0];
echo ' ';
echo $row[1];
It will display jess as the one with the high score and not bob:
Upvotes: 1
Views: 48
Reputation: 1070
Try this :-
SELECT firstname, lastname, english
FROM students
ORDER BY english DESC
LIMIT 1
Upvotes: 0
Reputation: 23480
When you use aggregate function you need to use GROUP BY
clause
SELECT firstname, lastname, MAX(english)
FROM students
GROUP BY lastname, firstname
From documentation
- If you use a group function in a statement containing no GROUP BY clause, it is equivalent to grouping on all rows.
Upvotes: 1
Reputation: 422
Try this:
$highest= mysql_query("SELECT firstname, lastname, english FROM students order by english DESC limit 1");
Upvotes: 2