0

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
Mahks
  • 6,441
  • 6
  • 28
  • 31

1 Answers1

2

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
mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194