# mysql 8.0 递归处理 (sample database classicmodels _No.4)

全表数据核对 ,行数据核对,列数据核对,Mysql 8.0 实例

可以去下载 classicmodels 数据库资源如下

点击:classicmodels官网下载

也可以去CSDN 去免费资源下载

前言

我们在做数据的时候 会碰到 部门表 ,员工表,这个一般是树型结构。我们先看下这个 全表的结构


一、查文档(recursion )

当然还是 看 mysql 文档了。

dev.mysql.com/doc/refman/...

recursion 中文解释就是 递归或者循环

二、准备

sql 复制代码
select * FROM classicmodels.employees t
可以理解  report to 就是 父ID 

三、递归处理

WITH RECURSIVE

sql 复制代码
WITH RECURSIVE cte (officeCode,employeeNumber,firstname,lastName, jobTitle,reportsTo ,tree,n ) AS (
    SELECT officeCode,employeeNumber,firstname,lastName, jobTitle,reportsTo, cast(employeeNumber as char(100)) as 'tree'
    , 1 as 'n'
    FROM classicmodels.employees where employeeNumber='1002'
    UNION all
    SELECT t.officeCode,t.employeeNumber,t.firstname,t.lastName, t.jobTitle ,t.reportsTo,
	CONCAT(cn.tree , '->', t.employeeNumber) AS 'tree',n+1 as 'n'
   	FROM classicmodels.employees t
  	INNER JOIN cte cn ON t.reportsTo = cn.employeeNumber
)
SELECT a.employeeNumber,  CONCAT(a.firstname,',',a.lastName) as 'name',
a.jobTitle,a.reportsTo , a.tree,a.n  
FROM cte a 

这个说明下 tree 列就是加的一列

一般的 RECURSIVE 是和 with 一起使用的。下面就是结果

sql 复制代码
也可以 将 
SELECT a.employeeNumber,  CONCAT(a.firstname,',',a.lastName) as 'name',
a.jobTitle,a.reportsTo , a.tree,a.n  
FROM cte a 

替换成 
SELECT a.employeeNumber,b.City,b.country,  CONCAT(a.firstname,',',a.lastName) as 'name',
a.jobTitle,a.reportsTo , a.tree,a.n  
FROM cte a left join classicmodels.offices b on a.officecode=b.officecode

四、提取

sql 复制代码
select tree,
if(n>=1, left(tree,4), null) as '1st',
if(n>=1 and length(tree)>4 ,  substring(tree,6,4), null) as '2st',
if(n>=1 and length(tree)>8 ,  substring(tree,11,4), null) as '3rd',
if(n>=1 and length(tree)>12 ,  substring(tree,16,4), null) as '4th'
from (

WITH RECURSIVE cte (officeCode,employeeNumber,firstname,lastName, jobTitle,reportsTo ,tree,n ) AS (
    SELECT officeCode,employeeNumber,firstname,lastName, jobTitle,reportsTo, cast(employeeNumber as char(100)) as 'tree'
    , 1 as 'n'
    FROM classicmodels.employees where employeeNumber='1002'
    UNION all
    SELECT t.officeCode,t.employeeNumber,t.firstname,t.lastName, t.jobTitle ,t.reportsTo,
	CONCAT(cn.tree , ',', t.employeeNumber) AS 'tree',n+1 as 'n'
   	FROM classicmodels.employees t
  	INNER JOIN cte cn ON t.reportsTo = cn.employeeNumber
)
SELECT a.employeeNumber,  CONCAT(a.firstname,',',a.lastName) as 'name',
a.jobTitle,a.reportsTo , a.tree,a.n  
FROM cte a) dd

截图结果就是 如下,这个基本就是数仓的结构了。

总结

sql 的 递归是个特殊的场景,作为一个做数据产品不建议运用在数据仓库,也不建议让数据开发或者数据分析提数处理。建议前端或者程序去实现递归可不是 sql

以上就是一些分享,谢谢大家喜欢 ,后面还是 有其他关于classicmodels 例子 。

相关推荐
小白勇闯网安圈12 小时前
Django 响应对象、文件上传与类视图
数据库·django·sqlite
努力的小雨12 小时前
同一份 SQL 跑多套环境:Ksql 变量怎么用才安全
数据库
科技绘图13 小时前
快鲸GEO vs 传统AI搜索优化:全链路自动化与高效内容生产在转化闭环上的对比
数据库·人工智能·自动化
ltl13 小时前
数据库作为 LLM 记忆体:语义缓存、RAG 与一致性
数据库
ltl13 小时前
WAL 与崩溃恢复:ARIES 协议怎么跑通
数据库
ltl13 小时前
TEE 数据库:EnclaveDB、Oblivious 原语与机密 SQL
数据库
麻瓜code14 小时前
【Mysql】重新学一遍 SQL 执行顺序
数据库·sql
这个DBA有点耶15 小时前
数据库一体机架构演进:从硬件堆叠到软硬深度耦合
服务器·网络·数据库·硬件架构·运维开发·database·数据库架构
一只fish15 小时前
优化器架构对比:Oracle vs PostgreSQL vs MySQL
mysql·postgresql·oracle
安_16 小时前
RAG的向量数据库:为LLM提供语义搜索能力
数据库