LEETCODE SQL题目 180. Consecutive Numbers

这个题目有个小技巧,就是使用了SQL里的用户自定义函数@

同时有个很值得注意的点是,()后加name命名用户自定义视图。

SELECT DISTINCT Num  AS ConsecutiveNums FROM (
  SELECT Num, COUNT(Rank) AS Cnt FROM (
    SELECT    Num,
      @curRank := @curRank + IF(@prevNum = Num, 0, 1) AS rank, @prevNum := Num
    FROM      Logs s, (SELECT @curRank := 0) r, (SELECT @prevNum := NULL) p
    ORDER BY  ID ASC
  ) t GROUP BY Rank HAVING Cnt >= 3 
) n;

你可能感兴趣的:(LeetCode,编程开发)