在重复数据mysql中显示最高值

时间:2015-02-01 10:13:08

标签: mysql

我有桌子

---------------------------------------------
id | name   | code   |  grade 
---------------------------------------------
1  | john   | ab12   | C
2  | john   | ab12   | D
3  | tom    | bb11   | B
4  | tom    | bb12   | A
5  | john   | ab12   | A
6  | alice  | ab12   | C 
7  | alice  | ab12   | D
8  | john   | bb11   | D
9  | john   | bb11   | C
---------------------------------------------

我希望显示名称为john

的值最高的数据
---------------------------------------------
id | name   | code   |  grade 
---------------------------------------------
1  | john   | ab12   | A
2  | john   | bb11   | C
---------------------------------------------

我从答案中尝试此代码,但无法正常工作。错误,没有结果 什么时候错了

$result=mysql_query("select code max(grade) as grade
from grade
where name = 'john'
group by code");

while($row=mysql_fetch_array($result))

{
   echo"<table border='1'>";
echo"<tr><td height='23'>$row[name]</td>";
echo"<td height='23'>$row[code]</td>";
echo"<td height='23'>$row[grade]</td></tr>";
echo"</table>";
} 

2 个答案:

答案 0 :(得分:1)

对于所有名称都

select name, code max(grade) as grade
from your_table
group by name, code

仅适用于John

select code, max(grade) as grade
from your_table
where name = 'John'
group by code

答案 1 :(得分:0)

回答

$result=mysql_query("select name,code, min(grade) as grade
from grade
where name='john'
group by code");

while($row=mysql_fetch_array($result))

{
    echo"<table border='1'>";
  echo"<tr>";
    echo"<td height='23'>$row[name]</td>";
            echo"<td height='23'>$row[code]</td>";
            echo"<td height='23'>$row[grade]</td>";
              echo"</tr>";
echo"</table>";

}