How can a run and its parent be read from the same table?
A self join aliases one relation twice so that one instance supplies a child and the other its parent.
SQLite example and interpretation
A retry points to its baseline run while the baseline has no parent. One join moves one generation. An arbitrary-depth lineage requires traversal.
WITH r(id,parent) AS (VALUES(1,NULL),(2,1)) SELECT c.id,p.id FROM r c LEFT JOIN r p ON p.id=c.parent ORDER BY c.id;Result [[1, null], [2, 1]]
中文
同一张表如何同时显示运行及其父运行?
自连接给同一关系两个别名,分别表示子记录与待匹配的父记录。
SQLite 示例与适用条件
基础运行没有父项,重试运行指向基础运行。一次连接只能取一代,追溯任意深度需递归遍历。
WITH r(id,parent) AS (VALUES(1,NULL),(2,1)) SELECT c.id,p.id FROM r c LEFT JOIN r p ON p.id=c.parent ORDER BY c.id;结果 [[1, null], [2, 1]]