zoukankan      html  css  js  c++  java
  • MySQL8的新特性CTE

    瞎BB

      十一长假的最后一天,想到明天要就回去上班了;内心的激动无法用平常的言语来表达,可能是国人的感情向来比较内敛(这个锅不能我一个人背)

      也可能是我们比较重行动(Just Do IT)。但... 我还是有写一些什么东西的必要得,那我今天就写两点吧!

      1): 趁着十一长假还没有过去,我在这里给大家拜个早年

      2): 讲讲MySQL-8.0.x的新特性(公共表表达式)CTE

    CTE有什么牛逼的地方

      1、从SQL的写法上看逻辑更加清晰

      2、直接递归

    CTE的语法格式

      CTE的主要思想就先生成临时结果集,以方便后面的使用;与临时表不同的是这个结果集的作用域不是当前session而是当前语句,对!不是

      session级是语句级别的

    with_clause:
        WITH [RECURSIVE]
            cte_name [(col_name [, col_name] ...)] AS (subquery)
            [, cte_name [(col_name [, col_name] ...)] AS (subquery)] ...

    例子一:朴素使用场景

      假设我们有一个Person表用来保存一些自然人的信息

    create table person(id int not null auto_increment primary key,name varchar(16));
    
    
    insert into person(name) values('Tom'),('Jry'),('Bob'),('Foo'),('Bar');
    
    select * from person;
    +----+------+
    | id | name |
    +----+------+
    |  1 | Tom  |
    |  2 | Jry  |
    |  3 | Bob  |
    |  4 | Foo  |
    |  5 | Bar  |
    +----+------+
    5 rows in set (0.00 sec)

      如果我们要查询id可以被2整除的那些行,通常我们可以这样写SQL

    select name from person where id % 2 = 0;
    +------+
    | name |
    +------+
    | Jry  |
    | Foo  |
    +------+
    2 rows in set (0.00 sec)

      如果出于某种不可描述的压力,一定要让你用CTE表达式来写的话,你也可以样混过去

    with ct(name) as (select name from person where id % 2 = 0) select name from ct;
    +------+
    | name |
    +------+
    | Jry  |
    | Foo  |
    +------+
    2 rows in set (0.00 sec)

      事实上CTE不只是可以用于select语句中,update,delete,insert也都是支持的,你只要知道CTE代表的是一个结果集就好了,通常就

      不会用错。

      还是出现某种不可描述的压力,要把所有id可以被2整除的公民的name改成"Hello SQL",CTE要硬上了哈!

    with ct(id) as (select name from person where id % 2 = 0) update person,ct set person.name="Hello SQL" where person.id = ct.id;
    Query OK, 2 rows affected (0.08 sec)
    Rows matched: 2  Changed: 2  Warnings: 0
    
    select * from person;
    +----+-----------+
    | id | name      |
    +----+-----------+
    |  1 | Tom       |
    |  2 | Hello SQL |
    |  3 | Bob       |
    |  4 | Hello SQL |
    |  5 | Bar       |
    +----+-----------+
    5 rows in set (0.00 sec)

    例子二:递归场景

      还是直接上比较学院派的例子吧、反正牛逼的我也想不出来!直接生成一个斐波那契数列吧!

    mysql> with recursive fib as (
        ->     select 0 as x, 1 as y 
        ->     union all
        ->     select 
        ->         fib.y as x,
        ->         fib.x+fib.y as y 
        ->     from fib where fib.x < 20)
        -> select y as n from fib;
    +------+
    | n    |
    +------+
    |    1 |
    |    1 |
    |    2 |
    |    3 |
    |    5 |
    |    8 |
    |   13 |
    |   21 |
    |   34 |
    +------+
    9 rows in set (0.00 sec)

    参考文档

    WITH Syntax (Common Table Expressions)

    学习交流

     -----------------------------http://www.sqlpy.com-------------------------------------------------

     -----------------------------http://www.sqlpy.com-------------------------------------------------

    ----

  • 相关阅读:
    docker添加sudo权限
    服务器出口ip
    flask
    ACM-奇特的立方体
    ACM-牛喝水
    ACM-可乐兑换
    ACM-Work Assignment
    ACM-DFS Template
    ACM-Checker Challenge
    ACM-Divide Tree
  • 原文地址:https://www.cnblogs.com/JiangLe/p/9750081.html
Copyright © 2011-2022 走看看