为什么以下查询不会增加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
答案 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