用户变量不递增

时间:2012-02-23 23:59:03

标签: mysql user-variables

为什么以下查询不会增加seq?

    SELECT   @sq :=if(@previous=uid,@sq,0)+1 as seq
            ,@previous:=uid as user
    FROM test

结果:

seq    user
1      111
1      111
1      111
1      222
1      222
1      222

1 个答案:

答案 0 :(得分:2)

你忘了在前面做set @sq:=0,@previous:=0吗?

或者,您可以随时将其加入查询:

SELECT   @sq :=if(@previous=uid,@sq,0)+1 as seq
        ,@previous:=uid as user
FROM test, (select @sq:=0, @previous:=0) foo