我有一个包含数千个条目的表,其中许多是层次结构形式。
我正在寻找一种方法将几个特定家族树的头部级别输入到查询中,搜索他们所有的孩子,他们孩子的孩子等,直到没有行,然后将每个id插入另一个表12次。
编号为(AID, id1, 1) (AID , id1, 2)...(AID, id1, 11)(AID, id1, 12)
我一直在尝试将此查询放在一起作为测试来帮助查找所需的id,最终目标是能够找到每个后代并将每个后代插入另一个表x12次。但收效甚微。有什么想法吗?
....for the right syntax to use near ''parent_id' FROM test AS t JOIN acc2 as r on t.parent_id = r.id; ' at line 25
是我当前的错误消息
(绝对业余)
DROP PROCEDURE IF EXISTS find_descendants ()
DELIMITER GO
CREATE PROCEDURE find_descendants ()
BEGIN
DROP TABLE IF EXISTS acc;
CREATE TEMPORARY TABLE acc
SELECT id, parent_id, 0 as level
FROM test
WHERE id = 2;
ALTER TABLE acc ADD primary key (id,parent_id);
REPEAT
DROP TABLE IF EXISTS acc2;
CREATE temporary table acc2
SELECT * FROM acc;
INSERT INTO acc2
SELECT t.id, r.'parent_id'
FROM test AS t
JOIN acc2 AS r ON t.parent_id = r.id;
UNTIL Row_count () = 0 END REPEAT;
END;
go
DELIMITER;
CALL find_descendants ();
SELECT * FROM acc;
在此之后,我收到错误#1137“无法重新打开表'acc2'”,这似乎使整个过程无效!
答案 0 :(得分:0)
INSERT查询中存在语法错误
INSERT INTO acc2
SELECT t.id, r.'parent_id' /*--COMMENT : remove '' from 'parent_id' , it should be r.parent_id */
FROM test AS t
JOIN acc2 AS r ON t.parent_id = r.id;