您需要使用參數作爲計數器。在下面的@r被只要構件是相同的前一行(如由命令所定義的)遞增對於每一行,如果它不是@r重置爲1相同的構件:
SELECT MemberID
FROM ( SELECT MemberID,
Position,
CreateDat,
@r:=IF(@m = MemberID, @r + 1, 1) AS Consec,
@m:= MemberID
FROM T,
(SELECT @r:= 0) r,
(SELECT @m:= 0) m
ORDER BY CreateDat DESC
) t
ORDER BY Consec DESC
LIMIT 1;
Example on SQL Fiddle
編輯
有輕微tweek你可以出去更多的信息,比如當連續週期的開始和結束:
SELECT MemberID, FirstCreateDat, CreateDat, Consec
FROM ( SELECT MemberID,
Position,
CreateDat,
@r:=IF(@m = MemberID, @r + 1, 1) AS Consec,
@d:=IF(@m = MemberID, @d, CreateDat) AS FirstCreateDat,
@m:= MemberID
FROM T,
(SELECT @r:= 0) r,
(SELECT @m:= 0) m,
(SELECT @d:= CAST(NULL AS DATETIME)) d
ORDER BY CreateDat DESC
) t
ORDER BY Consec DESC
LIMIT 1;
Example on SQL Fiddle
你有一個排序列別的地方?您需要在查詢的上下文中建立「連續」的含義。 –
我已經添加了一個created ...感謝 – Yannick
看看這個問題:[MySQL Specific Group by](http://stackoverflow.com/questions/18796096/mysql-specific-group-by/) –