1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| WITH cte (col1, col2) AS ( SELECT 1, 2 UNION ALL SELECT 3, 4 ) SELECT col1, col2 FROM cte;
with cte as ( select 1 as col1, 2 as col2 union all select 3, 4 ) select col1, col2 from cte;
with cte1 as (select 1) select * from (with cte2 as (select 2) select * from cte2 join cte1) as dt;
with recursive cte(n) as ( select 1 union all select n + 1 from cte where n < 5 ) select * from cte;
with recursive cte as ( select 1 as n, 'abc' as str union all select n + 1, concat(str, str) from cte where n < 3 ) select * from cte;
with recursive cte as ( select 1 as n, cast('abc' as char(20)) str union all select n + 1, concat(str, str) from cte where n < 3 ) select * from cte;
with recursive fibonacci(n, fib_n, next_fib_n) as ( select 1, 0, 1 union all select n + 1, next_fib_n, fib_n + next_fib_n from fibonacci where n < 10 ) select * from fibonacci where n = 8;
with recursive dates (date) as ( select sysdate() union all select date + interval 1 day from dates where date <= sysdate() + interval 10 day
) select * from dates;
use test; CREATE TABLE employees ( id INT PRIMARY KEY NOT NULL, name VARCHAR(100) NOT NULL, manager_id INT NULL, INDEX (manager_id), FOREIGN KEY (manager_id) REFERENCES employees (id) ); INSERT INTO employees VALUES (333, "Yasmina", NULL), # Yasmina is the CEO (manager_id is NULL) (198, "John", 333), # John has ID 198 and reports to 333 (Yasmina) (692, "Tarek", 333), (29, "Pedro", 198), (4610, "Sarah", 29), (72, "Pierre", 29), (123, "Adil", 692);
select * from employees;
with recursive employee_paths (id, name, path) as ( select id, name, cast(id as char(200)) from employees where manager_id is null union all select e.id, e.name, concat(ep.path, ',', e.id) from employee_paths as ep join employees as e on ep.id = e.manager_id ) select * from employee_paths order by path;
|