SELECT @rownum:=@rownum+1 AS 序號, t_user.* FROM t_user ,(SELECT @rownum:=0) r -- 最簡寫法1
更多寫法供參考:
① select (@rowNO := @rowNo+1) AS rowno,uid,uname from (SELECT * FROM t_user ) ;
② select (@rowNO := @rowNo+1) AS rowno,a.* from (SELECT * FROM t_user) a,(select @rowNO :=0) b ;
③ set @rn=0; select @rn:=@rn+1 as rn, m.* from (select * from t_user )m; -- 連續執行兩個語句
④ select (@rowNO := @rowNo+1) AS 序號,a.* from t_user a,(select @rowNO :=0) b -- 最簡寫法2
注意:@rownum的寫法不唯一,但一定有@,等於號寫法一定是:=(冒號等於)
以下寫法都可行:
SELECT @xxoo:=@xxoo+1 AS 序號, t_user.* FROM t_user ,(SELECT @xxoo:=0) r;
SELECT @1:=@1+1 AS 序號, t_user.* FROM t_user ,(SELECT @1:=0) r;
SELECT @:=@+1 AS 序號, a.* FROM t_user a ,(SELECT @:=0) r; (首推)
來張查詢結果圖:

