문제

트리에 새 노드를 삽입 할 때 폐쇄 테이블의 깊이 / 길이 열을 채우는 방법은 무엇입니까?

조상 및 자손의 값은 트리 구조로 배열 할 페이지를 나타내는 다른 테이블의 ID입니다.

클로저 테이블 :

ancestor    descendant     depth
1               1            0
1               2            1
1               3            1 
1               4            1
2               2            0
3               3            0 
4               4            0
.

조상과 자손을 올바르게 삽입하지만 깊이 열을 채우는 방법을 모르겠습니다. 삽입 쿼리 :

INSERT INTO closure_tree_path (ancestor, descendant)
SELECT ancestor, '{$node_id}' FROM closure_tree_path
WHERE descendant = '{$parent_id}'
UNION ALL SELECT '{$node_id}', '{$node_id}';
.

이것에 대해 가장 좋은 방법은 무엇입니까?감사합니다!

도움이 되었습니까?

해결책

깊이 + 1을 첫 번째 선택에 추가하십시오.

INSERT INTO closure_tree_path (ancestor, descendant, depth)
SELECT ancestor, '{$node_id}', depth+1 FROM closure_tree_path
WHERE descendant = '{$parent_id}'
UNION ALL SELECT '{$node_id}', '{$node_id}', 0;
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top