How do you use the “WITH” clause in MySQL?

前端 未结 7 1272
耶瑟儿~
耶瑟儿~ 2020-11-22 04:02

I am converting all my SQL Server queries to MySQL and my queries that have WITH in them are all failing. Here\'s an example:

WITH t1 AS
(
              


        
7条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 04:36

    Mysql Developers Team announced that version 8.0 will have Common Table Expressions in MySQL (CTEs). So it will be possible to write queries like this:

    
    WITH RECURSIVE my_cte AS
    (
      SELECT 1 AS n
      UNION ALL
      SELECT 1+n FROM my_cte WHERE n<10
    )
    SELECT * FROM my_cte;
    +------+
    | n    |
    +------+
    |    1 |
    |    2 |
    |    3 |
    |    4 |
    |    5 |
    |    6 |
    |    7 |
    |    8 |
    |    9 |
    |   10 |
    +------+
    10 rows in set (0,00 sec)
    

提交回复
热议问题