android Sqlite 递归查询 3.7.11 sqliteVersion

android Sqlite Recursive query on 3.7.11 sqliteVersion

你好,我必须使用递归查询来查找给定 child 的 parents 的层次结构 我用了

WITH  AllCategory(x) AS ( SELECT 'C1002' UNION ALL SELECT View_Cate.parentCode  FROM View_Cate, AllCategory  WHERE View_Cate.code=AllCategory.x AND View_Cate.parentCode IS NOT NULL )
 SELECT DISTINCT * FROM AllCategory

上面的查询工作正常,但因为我正在使用 minSdkVersion 16 我无法使用 RECURSIVE 所以我使用替代

SELECT t1.code AS lev1, t2.code as lev2, t3.code as lev3, t4.code as lev4,t5.code as lev4
FROM View_Cate AS t1
LEFT JOIN View_Cate AS t2 ON t2.code = t1.parentCode
LEFT JOIN View_Cate AS t3 ON t3.code = t2.parentCode
LEFT JOIN View_Cate AS t4 ON t4.code = t3.parentCode
LEFT JOIN View_Cate AS t5 ON t4.code = t4.parentCode
WHERE t1.code = 'C1003';

但上面的问题是我需要知道有多少级别的 parents 可用。在我的上下文中,没有定义 parents。谁能有其他解决这个问题的好方法请帮助我

递归 CTE 已添加到 SQLite,因为没有其他机制可以通过单个查询执行此操作。

您必须手动实施recursion algorithm,即重复读取父代码以获取以前的结果,直到您没有更多结果。