Home >Backend Development >PHP Tutorial >mysql - PHP 怎么调用统计数据库排名

mysql - PHP 怎么调用统计数据库排名

WBOY
WBOYOriginal
2016-06-06 20:13:401524browse

比如表里面的字段有2个,一个姓名,一个成绩

<code>1.name=张三 fs=87.5
2.name=张三 fs=70
3.name=李四 fs=85
4.name=李四 fs=90
</code>

我现在想统计一个排名,比如
“张三”的总分是87.5+70=157.5
“李四”的总分是85+90=175.5
175.5>157.5
李四第一名,张三第二名
我怎么得到这个1和2

回复内容:

比如表里面的字段有2个,一个姓名,一个成绩

<code>1.name=张三 fs=87.5
2.name=张三 fs=70
3.name=李四 fs=85
4.name=李四 fs=90
</code>

我现在想统计一个排名,比如
“张三”的总分是87.5+70=157.5
“李四”的总分是85+90=175.5
175.5>157.5
李四第一名,张三第二名
我怎么得到这个1和2

表结构和数据截图

mysql - PHP 怎么调用统计数据库排名

SQL语句

<code>SELECT name,sum(score) a from test GROUP BY name order by a desc</code>

有点看不惯fs这种字段命名,自作主张改成了score

<code>SELECT SUM(fs) as sumfs,name from demo GROUP BY name order by sumfs desc</code>

得到的数据结果就是按照总分数从高到低排序的

<code class="mysql">SELECT 
    @n := @n +1 n,
    name,
    sum(score) AS sum_score
FROM tbl, (SELECT @n := 0) m
GROUP BY name 
ORDER BY sum_score DESC</code>

Ref:
http://stackoverflow.com/questions/16555...

http://sqlfiddle.com/#!2/2d3a4/3

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn