计数,分组和&最大的PHP

时间:2014-09-03 02:10:19

标签: php mysql count group-by max

我有

|name  | number |
|andi  | 40     |
|irfan | 30     |
|lia   | 60     |
|andi  | 50     |
|lia   | 10     |
|sali  | 60     |

我想通过max

显示这个顺序
name   | number |
andi   | 90     |
lia    | 70     |
sali   | 60     |
irfan  | 30     |

我的代码如下所示:

SELECT name, 
       COUNT(name) 
FROM   tablename 
GROUP  BY name 
ORDER  BY MAX(number)

它不起作用。结果只按名称工作,而不是按最大数字

name   | number |
lia    | 70     |
sali   | 60     |
andi   | 90     |
irfan  | 30     |

3 个答案:

答案 0 :(得分:0)

你必须Select * from tablename Order By Number DESC

答案 1 :(得分:0)

您希望sum()不是count()

SELECT name, SUM(number)
FROM tablename
GROUP BY name
ORDER BY SUM(number) DESC;

答案 2 :(得分:0)

SELECT name, 
       sum(number) as score
FROM   Test2 
GROUP  BY name 
ORDER  BY score DESC

你要做的是 - 汇总列号' - 将其设为分数 - 按分数DESC订购

希望这能帮到你