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

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

WBOY
Release: 2016-06-06 20:13:40
Original
1505 people have browsed it

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

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

我现在想统计一个排名,比如
“张三”的总分是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>
Copy after login
Copy after login

我现在想统计一个排名,比如
“张三”的总分是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>
Copy after login

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

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

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

<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>
Copy after login

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

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template