当前位置:网站首页>with recursive用法
with recursive用法
2022-08-04 05:28:00 【顺毛黑起】
with recursive 则是一个递归的查询子句,他会把查询出来的结果再次代入到查询子句中继续查询。
with recursive d(n, fact) as (
values (1,2)
union all #合并
select n+1, (n+1)*fact from d where n < 5)
SELECT * from d;
递归过程如下:
n=1 fact=2
n=1,n<5: n=1+1=2,fact=(1+1)*2=4
n=2,n<5:n=2+1=3,fact=(2+1)*4=12
n=3,n<5:n=3+1=4,fact=(3+1)*12=48
n=4,n<5:n=4+1=5,fact=(4+1)*48=240
n=5 n>=5==stop
with recursive d(n, fact) as (
values (1,2)
union all
select n+2, (n+1)*fact from d where n < 5)
SELECT * from d;
递归过程如下:
n=1 fact=2
n=1,n<5: n=1+2=3,fact=(1+1)*2=4
n=3,n<5:n=3+2=5,fact=(3+1)*4=16
n=5 n>=5==stop
with recursive d(n, fact) as (
values (1,2)
union all
select n+2, (n+1)*fact from d where n < 5)
select sum(fact) from d;
sum(fact)=2+4+16=22
with recursive d(n, fact) as (
values (1,2)
union all
select n+2, (n+1)*fact from d where n < 5)
select sum(n) from d;
sum(n)=1+3+5=9
select * from company;
with recursive t(n) as (
values (10)
union all
select salary from company where salary < 20000
)
select * from t;
with recursive t(n) as (
values (10)
union all
select salary from company where salary < 20000
)
select sum(n) from t;
边栏推荐
猜你喜欢
随机推荐
IP地址查询
EPSON RC+ 7.0 使用记录一
将两个DataTable合并——DataTable.Merge 方法
自动化运维工具Ansible(5)流程控制
通过&修改数组中的值
CTFshow—Web入门—信息(1-8)
JNI基本使用
Embedded system driver primary [4] - under the basis of character device driver _ concurrency control
关于事件捕获和事件冒泡的顺序,以及如何处理事件冒泡带来的影响
判断字符串是否有子字符串重复出现
Handling List
Swoole学习(二)
MediaCodec支持的类型
自己学习爬虫写的基础小函数
CAS与自旋锁、ABA问题
webtrc 中VideoAdapter类中的作用及局限
页面刷新没有执行watch?
详解“Node实现数据加密”过程
原型对象及原型链的理解
自动化运维工具Ansible(4)变量