SQLite 中的递归

前端之家收集整理的这篇文章主要介绍了SQLite 中的递归前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
create table if not exists testTable( id INT PRIMARY KEY,[name] NVARCHAR(20),parentId INT );
INSERT INTO testTable(id,[name],parentId) VALUES(1,'xf1',0);
INSERT INTO testTable(id,parentId) VALUES(2,'xf2',parentId) VALUES(3,'xf3',2);
INSERT INTO testTable(id,parentId) VALUES(4,'xf4',3);
INSERT INTO testTable(id,parentId) VALUES(5,'xf5',4);
INSERT INTO testTable(id,parentId) VALUES(6,'xf6',5);

WITH RECURSIVE 
  cte(id,name,parentId) AS(
    SELECT id,parentId FROM testTable WHERE id='6'
    UNION ALL
    SELECT a.id,a.name,a.parentId FROM testTable AS a INNER JOIN 
        cte ON a.id=cte.parentId   
)
SELECT * FROM cte;



--得到1-10 的数
WITH RECURSIVE
  cnt(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM cnt WHERE x<10)
SELECT x FROM cnt order by x;

WITH RECURSIVE
  cnt(x) AS (
     SELECT 1
     UNION ALL
     SELECT x+1 FROM cnt
      LIMIT 10
  )
SELECT x FROM cnt order by x;


参考:点击打开链接

原文链接:https://www.f2er.com/sqlite/198220.html

猜你在找的Sqlite相关文章