第一种方法:
select (@i:=@i+1) as i,table_name.* from table_name,(select @i:=0) as it
第二种方法:
set @rownum=0;
select @rownum:=@rownum+1 as rownum, t.username from auth_user t limit 1,5;
如为publish表中publishid大于40的行重新设置序号(order字段)
set @rownum=0;
update publish set `order`= @rownum:=@rownum+1 where `publishid`>40
测试会不会影响sql查询性能
explain select (@i:=@i+1) as i, tblMybook.id from tblMybook,(select @i:=0) as it where subject=1 having i%40000=0;
分析结果可以用到索引subject
——实验1 只添加序号—————结果:成功———————
select (@i:=@i+1) as i,tblMybook.id from tblMybook,(select @i:=0) as it where 1 ;
iid
11
22
33
44
55
66
77
88