1.法一:

select  (@i:=@i+1)  i,a.url from  base_api_resources a  ,(select   @i:=0)  t2 order by a.id desc limit 10;

2.法二:

set @rownum=0;
select @rownum:=@rownum+1 as rownum, a.url from base_api_resources a order by a.id desc limit 10

上述两种方法原理就是,事先定义一个变量,通过变量的递加以及虚拟表的联查达到生成序号列的目的。

说明:mysql中变量不用事前申明,在用的时候直接用“@变量名”使用就可以了。

   第一种用法:set @i=1; 或set @i:=1; //这里要使用变量来保存数据,直接使用@num变量

   第二种用法:select @i:=1; 或 select @i:=字段名 from 表名 where …

 注意上面两种赋值符号,使用set时可以用“=”或“:=”,但是使用select时必须用“:=赋值”
posted on 2018-10-30 17:28  chengloading  阅读(1043)  评论(0)    收藏  举报