当前位置:网站首页>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;

边栏推荐
猜你喜欢

MySql的concat和group_concat的区别

自己学习爬虫写的基础小函数
![Deploy LVS-DR cluster [experimental]](/img/ad/84e05a6421d668b0b6ba6eeba0c730.jpg)
Deploy LVS-DR cluster [experimental]

详解“Node实现数据加密”过程

实际开发中,如何实现复选框的全选和不选

C language -- operator details

npm install dependency error npm ERR! code ENOTFOUNDnpm ERR! syscall getaddrinfonpm ERR! errno ENOTFOUND

Delphi-C side interesting menu operation interface design

Swoole学习(一)

Commons Collections2
随机推荐
webtrc 中VideoAdapter类中的作用及局限
自动化运维工具Ansible(4)变量
剑指 Offer 2022/7/1
IP地址查询
Redis持久化方式RDB和AOF详解
【Matlab仿真】:一带电量为q的电荷以速度v运动,求运动电荷产生磁感应强度
k9s-终端UI工具
对象存储-分布式文件系统-MinIO-3:MinIo Client(mc)
js 基础学习笔记
网络大作业心得笔记
phpexcel导出数据为xml
flink sql left join数据倾斜问题解决
实际开发中,客户要求密码输入框禁止粘贴~
自动化运维工具Ansible(3)PlayBook
Set集合与Map集合
SQL练习 2022/7/5
Embedded system driver primary [4] - under the basis of character device driver _ concurrency control
多个gcc/glibc版本的共存及指定gcc版本的编译
MediaCodec支持的类型
Upload靶场搭建&&第一二关