输入: Logs 表: +----+-----+ | Id | Num | +----+-----+ |1|1| |2|1| |3|1| |4|2| |5|1| |6|2| |7|2| +----+-----+ 输出: Result 表: +-----------------+ | ConsecutiveNums | +-----------------+ |1| +-----------------+ 解释:1 是唯一连续出现至少三次的数字。
解法:
1 2 3 4 5 6
SELECTDISTINCT l1.Num AS ConsecutiveNums FROM logs l1, logs l2, logs l3 WHERE l1.Id = l2.Id -1 AND l2.Id = l3.Id -1 AND l1.Num = l2.Num AND l2.Num = l3.Num;