如何在sqlite数据库中获取树并将节点转换为从它到根的路径?

问题描述 投票:0回答:1

我有一个SQLite数据库,其中包含一个表示树的表。表中的每一行表示除了链接到自身的第一个节点之外的两个节点之间的关系。

基本上给出了这张表

BEGIN TRANSACTION;
CREATE TABLE "unnamed" (key TEXT PRIMARY KEY, value TEXT);
INSERT INTO `unnamed` (key,value) VALUES ('1','1');
INSERT INTO `unnamed` (key,value) VALUES ('2','1');
INSERT INTO `unnamed` (key,value) VALUES ('3','10');
INSERT INTO `unnamed` (key,value) VALUES ('10','5');
INSERT INTO `unnamed` (key,value) VALUES ('5','16');
INSERT INTO `unnamed` (key,value) VALUES ('16','8');
INSERT INTO `unnamed` (key,value) VALUES ('8','4');
INSERT INTO `unnamed` (key,value) VALUES ('4','2');
INSERT INTO `unnamed` (key,value) VALUES ('6','3');
INSERT INTO `unnamed` (key,value) VALUES ('7','22');
INSERT INTO `unnamed` (key,value) VALUES ('22','11');
INSERT INTO `unnamed` (key,value) VALUES ('11','34');
INSERT INTO `unnamed` (key,value) VALUES ('34','17');
INSERT INTO `unnamed` (key,value) VALUES ('17','52');
INSERT INTO `unnamed` (key,value) VALUES ('52','26');
INSERT INTO `unnamed` (key,value) VALUES ('26','13');
INSERT INTO `unnamed` (key,value) VALUES ('13','40');
INSERT INTO `unnamed` (key,value) VALUES ('40','20');
INSERT INTO `unnamed` (key,value) VALUES ('20','10');
INSERT INTO `unnamed` (key,value) VALUES ('9','28');
INSERT INTO `unnamed` (key,value) VALUES ('28','14');
INSERT INTO `unnamed` (key,value) VALUES ('14','7');
COMMIT;

输出此表

+------+------------------------------------------------------+
| Node | Path                                                 |
+------+------------------------------------------------------+
|    1 | 1                                                    |
|    2 | 2-1                                                  |
|    3 | 3-10-5-16-8-4-2-1                                    |
|    4 | 4-2-1                                                |
|    5 | 5-16-8-4-2-1                                         |
|    6 | 6-3-10-5-16-8-4-2-1                                  |
|    7 | 7-22-11-34-17-52-26-13-40-20-10-5-16-8-4-2-1         |
|    8 | 8-4-2-1                                              |
|    9 | 9-28-14-7-22-11-34-17-52-26-13-40-20-10-5-16-8-4-2-1 |
|   10 | 10-5-16-8-4-2-1                                      |
|   11 | 11-34-17-52-26-13-40-20-10-5-16-8-4-2-1              |
|   13 | 13-40-20-10-5-16-8-4-2-1                             |
|   14 | 14-7-22-11-34-17-52-26-13-40-20-10-5-16-8-4-2-1      |
|   16 | 16-8-4-2-1                                           |
|   17 | 17-52-26-13-40-20-10-5-16-8-4-2-1                    |
|   20 | 20-10-5-16-8-4-2-1                                   |
...

我一直在阅读有关WITHWITH RECURSIVE的文章,但我无法理解他们的工作方式。

sql sqlite tree hierarchy
1个回答
1
投票

此解决方案构建从leaf到root的路径:

WITH RECURSIVE
  queue(leaf,head,path) AS (
    SELECT CAST(key AS INTEGER) AS leaf, key AS head, key AS path
      FROM unnamed
    UNION
    SELECT queue.leaf AS leaf, unnamed.value AS head, (queue.path||'-'||unnamed.value) AS path
      FROM unnamed, queue
      WHERE unnamed.value != queue.head AND unnamed.key = queue.head
  )
SELECT leaf AS node, path AS path
  FROM queue
  WHERE head = 1
  -- WHERE length(path) = (SELECT MAX(LENGTH(path)) FROM queue AS q WHERE q.leaf = queue.leaf)
  ORDER BY leaf;

请阅读此处的文档:http://www.sqlite.org/lang_with.html

initial-select将所有键作为叶子和头部和路径添加到队列中。对于ORDER BY,Leaf将被转换为整数,head是当前头,并且路径将逐步扩展:

SELECT CAST(key AS INTEGER) AS leaf, key AS head, key AS path
    FROM unnamed

对于每个队列项,将查询数据库以将头部的路径扩展到树的根。因此队列项的头部必须匹配一个键,该键不是树的根。

WHERE unnamed.value != queue.head AND unnamed.key = queue.head

两者的组合路径用短划线扩展并添加到队列中:

SELECT queue.leaf AS leaf, unnamed.value AS head, (queue.path||'-'||unnamed.value) AS path
    FROM unnamed, queue
    WHERE unnamed.value != queue.head AND unnamed.key = queue.head

结果包含从leaf到root的所有路径,包括所有中间结果。因此,我们只选择那些以头/键值1结束于根节点的那些。

SELECT leaf AS node, path AS path
    FROM queue
    WHERE head = 1
    ORDER BY leaf;

或者,您也可以选择路径最长的那些。

SELECT leaf AS node, path AS path
    FROM queue
    WHERE length(path) = (SELECT MAX(LENGTH(path)) FROM queue AS q WHERE q.leaf = queue.leaf)
    ORDER BY leaf;
© www.soinside.com 2019 - 2024. All rights reserved.