Home > Database > Mysql Tutorial > MySQL中给自定义的字段查询结果添加排名的方法_MySQL

MySQL中给自定义的字段查询结果添加排名的方法_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-01 12:59:17
Original
898 people have browsed it

 我正在用 MySQL 客户端的时候,突然想到如果可以给查询结果添加排名该多好啊,然后就找到了一个简单的解决办法。

201562694257887.png (414×129)

下面是一个示例表的数据:
 然后我们要根据 Roll_No 字段进行排序并给出排名,我们首先必须定义一个初始值为0的变量,然后在查询结果中使用这个变量。

如下面的代码:
 

SET @counter=0;
 
SELECT @counter:=@counter+1 AS Rank,LastName,Roll_no as Roll FROM Students ORDER BY Roll_
Copy after login

执行该语句的结果如下:

201562694335135.png (206×122)

更多关于自定义变量的内容请看 MySQL 的文档。

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
Latest Issues
MySQL stops process
From 1970-01-01 08:00:00
0
0
0
Error when installing mysql on linux
From 1970-01-01 08:00:00
0
0
0
phpstudy cannot start mysql?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template