Home >Database >Mysql Tutorial >SQL&MySQL查询添加字段为行号_MySQL

SQL&MySQL查询添加字段为行号_MySQL

WBOY
WBOYOriginal
2016-06-01 13:37:351388browse

bitsCN.com


SQL&MySQL查询添加字段为行号

 

SQL:

select row_number() over(order by col desc) ,* from table;

 

函数“row_number”必须有使用 ORDER BY 的 OVER 子句,按照字段col来排

列自动取值

MySQL:

 

SET @r=0;

SELECT @r:=IFNULL(@r,0)+1 AS rownum,table.* FROM table ORDER BY col DESC;

 

与SQL不同,为循环添加字段,如果查询所有行必须加上表名table.*,若取某个

字段则不需要,col1,col2...

 

bitsCN.com
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