统计连续三次出现的数字
- Mysql & SqlServer

查询语句:适用Mysql&SqlServer
方式一:使用开窗函数LEAD。
SELECT distinct num1 FROM
(
SELECT num num1, LEAD(num,1) over (ORDER BY id) num2, LEAD(num,2) over (ORDER BY id) num3 from tb_test1
) t1
WHERE num1 = num2 and num2=num3;
方式二:确保 t2 行的 id 值比 t1 行的 id 值大 1,t3 行的 id 值比 t2 行的 id 值大 1。并且确保 t1 行和 t2 行的 num 值相同,t2 行和 t3 行的 num 值相同。
SELECT DISTINCT t1.num
FROM tb_test1 t1
JOIN tb_test1 t2 ON t1.id = t2.id - 1 AND t1.num = t2.num
JOIN tb_test1 t3 ON t2.id = t3.id - 1 AND t2.num = t3.num;
查询结果:

人生如逆旅
我亦是行人

浙公网安备 33010602011771号