0
我有一個SQLite數據庫和一個表,表示一棵樹。表中的每一行都表示除了鏈接到自身的第一個節點之外的兩個節點之間的關係。如何在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 |
...
我一直在閱讀有關WITH
和WITH RECURSIVE
,但我不能讓我的頭周圍他們如何工作。
[documentation](http://www.sqlite.org/lang_with.html)解釋了它們的工作原理。你不明白什麼具體的部分? –