나무의 첫 번째 일치 노드로 결과를 제한하기 :내가 acts_as_ordered_tree 보석에서이 SQL 출력이
이 쿼리 내가왔다 component_instances을 보여주고 싶은 휴지통위한 것입니다SELECT "component_instances".*
FROM "component_instances"
INNER JOIN (
WITH RECURSIVE descendants AS (
SELECT id, parent_id, ARRAY[position] AS _positions
FROM "component_instances"
WHERE "component_instances"."id" = 1
UNION ALL
SELECT alias1.id, alias1.parent_id, _positions || alias1.position
FROM descendants
INNER JOIN "component_instances" alias1
ON alias1.parent_id = descendants.id
)
SELECT * FROM descendants
) AS descendants
ON descendants.id = component_instances.id
WHERE ("component_instances"."id" != 1)
AND (component_instances.archived_at IS NOT NULL
AND component_instances.archive_number IS NOT NULL)
ORDER BY descendants._positions ASC
'아카이브 됨'(쿼리 마지막의 마지막 조건 참조) 원래 삭제 된 상위 항목을 먼저 복원하지 않으면 이러한 노드를 논리적으로 복원 할 수 없기 때문에 보관 된 component_instance의 하위 항목을 표시하고 싶지 않습니다.
이 쿼리를 수정하는 동안 문제가 발생하여 보관 노드가 발견 될 때 반복되지 않지만 여전히 결과에 해당 노드가 포함됩니다.
감사합니다. 멋지고 간결하며 완벽하게 작동합니다. 원래 쿼리의 마지막 부분을 중단하여 쿼리에서 삭제 된 부모보다 상위의 부모도 반환하도록했습니다. 내 쿼리의 목적은 삭제 된 (보관 된) 노트에서 자녀를 뺀 값만 반환하는 것이 었습니다. 나머지 쿼리를 끝에 추가하는 것만으로이 문제를 해결할 수 있습니다. 이것을 보여주기 위해 답을 업데이트하고 싶습니까? –