我们常常需要在查询中使用辅助的连续数字列 在mssql中 我们经常可以看到
sql2000 select identity(int,1,1) as id ,* into #t   from tb 
sql2005s select rn=row_number()over(),* from tb 这样类似的技术处理手段 通过id rn这样的连续数字辅助列进行查询处理 但是mysql中好像没有类似的查询生成连续数字手段 auto_increment 好像只能建表的时候用 我的问题就是:
我们想要在查询中使用辅助的生成的连续数字列该怎办?我想到的就是
建立一个有auto_increment属性的表和想要的字段的表,然后在这个表上处理 谢谢~

解决方案 »

  1.   

    http://blog.csdn.net/ACMAIN_CHM/archive/2009/04/20/4095531.aspx
    MySQL中的ROWNUM的实现 
      

  2.   

    老大 你的第二个变量方法 没有生成连续的排列数字啊第三个方法 万一没有类似col一样的有大小可以比的列怎办?
    比如 我的列值如下
    1
    2
    4
    3
    我要生成
    col id
    1    1
    2    2
    4    3
    3    4第四个方法也对上面的情况不行 
      

  3.   

    set @num=0;
    select *,@num:=@num+1 from tt
      

  4.   


    大大试过么?
    ..
    我执行失败..
    mysql> create table t(
        -> col int);
    Query OK, 0 rows affected (0.06 sec)mysql> insert t select
        -> 1 union all selct
        -> 2 union all select
        -> \c
    mysql> insert t select
        -> 1 union all select
        -> 5 union all select
        -> 3;mysql> set @num=0;
    Query OK, 0 rows affected (0.00 sec)mysql> select *,@num=:@num+1 from t;
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
    corresponds to your MySQL server version for the right syntax to use near ':@num
    +1 from t' at line 1
      

  5.   

     select *,@num=:@num+1 from t; select *,@num:=@num+1 from t;