TAGS :Viewed: 18 - Published at: a few seconds ago

[ User variable not incrementing ]

Why would the following query not increment seq?

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

Result:

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

Answer 1


You forgot to do set @sq:=0,@previous:=0 at the front?

Alternatively you can always join it in to the query:

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