一.從test02表中查詢每個uid最早登錄的前2天
表如下圖所示:
select * from (select uid,day, @ROW := case when @uuid=uid then @ROW+1 else 1 END rn, @uuid:=uid from (select uid,day from test02 where day between '2019-09-01' and '2019-09-30' order by uid,day) l2, (select @ROW:=0 ,@uuid:=0) l3 ) l4 where rn <=2
運行結果如下圖:
簡單解釋:@ROW:=0 ,@uuid:=0 相當於2個變量
當@uuid=uid時代表為同一用戶,@ROW可以理解為行號
用戶不同時,@ROW再次為1
注意:一定要提前做好排序