180. Consecutive Numbers(SQL)
# Medium
Solution 1:
复制三个表格做内交,想象一下三张卡片,第二张比第一张往下位移1格,第三张比第一张往下位移2格,根据num
相等相交出来的一行一定是连续重复了三次的数
Solution 2:
建立三个表格,加上4个所有的限制条件,即为结果
# Write your MySQL query statement below
select distinct l1.Num ConsecutiveNums from Logs l1
join Logs l2 on l1.Id = l2.Id-1
join Logs l3 on l1.Id = l3.Id-2
where l1.Num = l2.Num and l2.Num = l3.Num;
# 或者另一种方法
select distinct l1.Num 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;
从这道题可以看出命名的规则比较明显,无论是表格还是列的命名都直接紧跟后面即可,表格重命名可以在前面的select关键词后面直接用
Last updated
Was this helpful?