it编程 > 数据库 > Mysql

MySQL CTE (Common Table Expressions)示例全解析

4人参与 2025-07-26 Mysql

cte (common table expression,公共表表达式) 是 mysql 8.0 引入的重要特性,它允许在查询中创建临时命名结果集,提高复杂查询的可读性和可维护性。

基本语法

with cte_name as (
    select ...  -- cte查询定义
)
select * from cte_name;  -- 主查询

cte 主要特点

非递归 cte

简单 cte 示例

with department_stats as (
    select 
        department_id, 
        count(*) as employee_count,
        avg(salary) as avg_salary
    from employees
    group by department_id
)
select 
    d.department_name,
    ds.employee_count,
    ds.avg_salary
from departments d
join department_stats ds on d.department_id = ds.department_id;

多 cte 示例

with 
high_earners as (
    select * from employees where salary > 100000
),
it_employees as (
    select * from employees where department_id = 10
)
select 
    h.employee_id,
    h.name,
    'high earner' as category
from high_earners h
union all
select 
    i.employee_id,
    i.name,
    'it employee' as category
from it_employees i;

递归 cte

递归 cte 可以处理层次结构数据,如组织结构、评论树等。

基本递归 cte 结构

with recursive cte_name as (
    -- 基础部分(种子查询)
    select ... where ...
    union [all]
    -- 递归部分
    select ... from cte_name join ...
    where ...
)
select * from cte_name;

递归 cte 示例:组织结构查询

with recursive org_hierarchy as (
    -- 基础部分:查找顶级管理者
    select 
        employee_id,
        name,
        manager_id,
        1 as level
    from employees
    where manager_id is null
    union all
    -- 递归部分:查找下属员工
    select 
        e.employee_id,
        e.name,
        e.manager_id,
        oh.level + 1
    from employees e
    join org_hierarchy oh on e.manager_id = oh.employee_id
)
select * from org_hierarchy order by level, employee_id;

递归 cte 示例:生成序列

with recursive number_sequence as (
    select 1 as n
    union all
    select n + 1 from number_sequence where n < 10
)
select * from number_sequence;

cte 的优势

cte 与派生表的比较

特性cte派生表
可读性
可重用性可在查询中多次引用每次使用都需要重新定义
递归支持支持不支持
性能通常更好可能较差
语法清晰度更清晰嵌套较深时难以理解

实际应用场景

with 
monthly_sales as (
    select 
        date_format(order_date, '%y-%m') as month,
        sum(amount) as total_sales
    from orders
    group by month
),
growth_rate as (
    select 
        month,
        total_sales,
        lag(total_sales) over (order by month) as prev_sales,
        (total_sales - lag(total_sales) over (order by month)) / 
        lag(total_sales) over (order by month) * 100 as growth_pct
    from monthly_sales
)
select * from growth_rate;
with 
raw_data as (
    select * from source_table where quality_check = 1
),
cleaned_data as (
    select 
        id,
        trim(name) as name,
        case when age < 0 then null else age end as age
    from raw_data
)
select * from cleaned_data;
with recursive path_finder as (
    select 
        start_node as path,
        start_node,
        end_node,
        1 as length
    from graph
    where start_node = 'a'
    union all
    select 
        concat(pf.path, '->', g.end_node),
        g.start_node,
        g.end_node,
        pf.length + 1
    from graph g
    join path_finder pf on g.start_node = pf.end_node
    where find_in_set(g.end_node, replace(pf.path, '->', ',')) = 0
)
select * from path_finder;

性能考虑

set session cte_max_recursion_depth = 2000;
with cte_name as (
    select /*+ merge() */ * from table_name
)
select * from cte_name;

限制

cte是mysql中处理复杂查询的强大工具,合理使用可以显著提高sql代码的可读性和维护性。

到此这篇关于mysql cte (common table expressions) 详解的文章就介绍到这了,更多相关mysql cte内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

您想发表意见!!点此发布评论

推荐阅读

2025版mysql8.0.41 winx64 手动安装详细教程

07-26

MySQL中DROP、DELETE与TRUNCATE的对比分析

07-26

Nginx中指令server_name的详细使用指南

07-25

MySQL 无监听端口故障问题排查记录

07-25

MySQL数据表添加字段的三种方式总结

07-25

MySQL添加索引的5种常用方式总结(附实用SQL代码)

07-25

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论