SQL:With recursive 递归用法

With Recursive as

有两种递归字段n的声明写法,第一种是在with… as 中声明需要递归的字段,第二种是在sql语句中第一段的初始值内声明变量。
WITH RECURSIVE cte (n) AS
( select 初始值 from table
union all
select 递归内容 from cte where (终止条件)
)
这里注意 递归内容中选择的表格是cte,引用的是临时的递归表格。

# 在with... as 中声明需要递归的字段
WITH RECURSIVE cte (n) AS  
(
  SELECT 1  #初始值
  UNION ALL
  SELECT n + 1 FROM cte WHERE n < 5  #递归内容 from cte 表
)
SELECT * FROM cte;

#输出结果
+------+
| n    |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
|    5 |
+------+



# 在第一段的初始值代码中声明
WITH RECURSIVE cte AS
(
  SELECT 1 AS n, CAST('abc' AS CHAR(20)) AS str
  UNION ALL
  SELECT n + 1, CONCAT(str, str) FROM cte WHERE n < 3
)
SELECT * FROM cte;

#输出结果
+------+--------------+
| n    | str          |
+------+--------------+
|    1 | abc          |
|    2 | abcabc       |
|    3 | abcabcabcabc |
+------+--------------+

注:hive 目前不支持

参考链接:https://dev.mysql.com/doc/refman/8.0/en/with.html#common-table-expressions-recursive

题目一

Column NameType
customer_idint
customer_namevarchar
customer_id 是该表主键.
该表第一行包含了顾客的名字和id.

写一个 SQL 语句, 找到所有遗失的顾客id. 遗失的顾客id是指那些不在 Customers 表中, 值却处于 1 和表中最大 customer_id 之间的id.
注意: 最大的 customer_id 值不会超过 100.

返回结果按 ids 升序排列

查询结果格式如下例所示。

输入:
Customers 表:

customer_idcustomer_name
1Alice
4Bob
5Charlie

输出:

ids
2
3

**解释:**表中最大的customer_id是5, 所以在范围[1,5]内, ID2和3从表中遗失.

拆解思路

题目很简单,主要是需要生成一个1到最大值的临时表,然后从里面找出不再customer 表格里的id。

本题难点在于如何生成这个临时表。

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)

来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/find-the-missing-ids

题目二: 向CEO汇报的所有员工

员工表:Employees

Column NameType
employee_idint
employee_namevarchar
manager_idint

employee_id 是这个表的主键。
这个表中每一行中,employee_id 表示职工的 ID,employee_name 表示职工的名字,manager_id 表示该职工汇报工作的直线经理。
这个公司 CEO 是 employee_id = 1 的人。

拆解思路

# Write your MySQL query statement below
with recursive temp as (
    select e.employee_id from Employees e where e.employee_id!=1 and manager_id=1
    union all 
    select e.employee_id from Employees e join temp t on t.employee_id=e.manager_id
)
select * from temp 

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/all-people-report-to-the-given-manager

WITH RECURSIVESQL Server中递归查询的一种方式,也称为CTE递归查询。递归查询是指在一个表中对自身进行查询,直到查询结果满足条件为止。在SQL Server中使用WITH RECURSIVE可以实现递归查询。WITH RECURSIVE的语法如下: ``` WITH recursive_cte (col1, col2, col3, ...) AS ( --初始查询语句 SELECT col1, col2, col3, ... FROM table_name WHERE condition UNION ALL --递归查询语句 SELECT col1, col2, col3, ... FROM table_name JOIN recursive_cte ON recursive_cte.col = table_name.col WHERE condition ) SELECT * FROM recursive_cte; ``` 其中,recursive_cte是递归公共表达式的名称,col1、col2、col3是要查询的列,table_name是要查询的表名,condition是查询条件。在WITH RECURSIVE中,包含两个查询语句:初始查询语句和递归查询语句。初始查询语句用于筛选出符合条件的记录,递归查询语句用于在初始查询语句的基础上对自身进行递归查询。 需要注意的是,WITH RECURSIVE中的递归查询语句必须包含UNION ALL关键字,并且UNION ALL后面的SELECT语句必须引用了递归公共表达式recursive_cte。如果不包含UNION ALL或者引用了其他表,则会导致死循环。 以下是一个WITH RECURSIVE的例子: ``` WITH recursive_cte (employee_id, manager_id, level) AS ( -- 初始查询语句 SELECT employee_id, manager_id, 0 as level FROM employee_table WHERE employee_id = 1 UNION ALL -- 递归查询语句 SELECT employee_table.employee_id, employee_table.manager_id, recursive_cte.level + 1 FROM employee_table JOIN recursive_cte ON recursive_cte.manager_id = employee_table.employee_id ) SELECT * FROM recursive_cte; ``` 这个例子用于查询一个员工及其直接或间接的所有上级领导。在初始查询语句中,我们选择了员工ID为1的员工作为起点。在递归查询语句中,我们通过JOIN连接到employee_table表中,并使用recursive_cte中的level来计算每个员工的级别。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值