MAX功能不起作用

时间:2014-03-08 14:35:31

标签: php mysql

我有这张桌子:

 fistname      | lastname     |  english 
--------------------------------------------- 
bob           | wilson       | 77  
jess          | farr         | 47 

这是我在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 '&nbsp';
echo $row[1];

它将显示jess为高分而不是bob:

3 个答案:

答案 0 :(得分:2)

试试这个:

$highest= mysql_query("SELECT firstname, lastname, english FROM students order by english DESC limit 1");

答案 1 :(得分:1)

使用聚合函数时,需要使用GROUP BY子句

SELECT firstname, lastname, MAX(english)
FROM students
GROUP BY lastname, firstname

来自documentation

  
      
  • 如果在不包含GROUP BY子句的语句中使用组函数,则它等同于对所有行进行分组。
  •   

答案 2 :(得分:0)

试试这个: -

SELECT firstname, lastname, english
FROM students
ORDER BY english DESC
LIMIT 1