找到遗失的id
做这道题时,学到了recursive的使用方法
sql中with xxxx as () 是对一个查询子句做别名,同时数据库会对该子句生成临时表
with recursive 则是一个递归的查询子句,它会把查询出来的结果再次代入到查询子句中继续查询。
可以这样理解,dep=n+1是dep=n的递归调用sql
with recursive t1 as (
select 1 as n
union all
select n + 1 from t1 where n < 100
)
select n as ids
from t1
where
n <= (select max(customer_id)from Customers)
and n not in (select customer_id from Customers)