Mahks
Mahks

Reputation: 6779

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

Upvotes: 0

Views: 110

Answers (1)

mathematical.coffee
mathematical.coffee

Reputation: 56935

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

Upvotes: 2

Related Questions