您需要使用参数作为计数器。在下面的@r 中,只要成员与前一行相同(由 order by 定义),每行都会递增,如果不是相同的成员,@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;
SQL Fiddle 示例
编辑
只需一周时间,您就可以获得更多信息,例如连续期间的开始和结束时间:
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;
SQL Fiddle 示例