当前位置:网站首页>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;
边栏推荐
猜你喜欢
随机推荐
Commons Collections1
二月、三月校招面试复盘总结(二)
[NSSRound#1 Basic]
keep-alive的使用及详解
智能合约安全——溢出漏洞
判断字符串是否有子字符串重复出现
自动化运维工具Ansible(3)PlayBook
Deploy LVS-DR cluster [experimental]
乱码解决方案
NFT市场可二开开源系统
lambda函数用法总结
k3s-轻量级Kubernetes
OpenGLES 学习之帧缓存
Set集合与Map集合
Lombok的一些使用心得
Shell(2)数值运算与判断
php实现telnet访问端口
程序员的财富观
flink-sql所有表格式format
基于C语言的学生信息管理系统_(更新版)_(附源码和安装包)_课程设计_**往事随風**的博客