次のPostgreSQL9.2.2テーブルを使用します。
id | parent_id
--------+----------
body | null
head | body
mouth | head
eye | head
tooth | mouth
tongue | mouth
sclera | eye
cornea | eye
親ではないすべての子について、すべての直接の間接的な親がリストされている出力が必要です。
tooth mouth
tooth head
tooth body
tongue mouth
tongue head
tongue body
sclera eye
sclera head
sclera body
cornea eye
cornea head
cornea body
検索してみましたが、結果にはwith-recursiveを使用した単一アイテムのクエリのみが表示されました。
WITH RECURSIVE t AS (
SELECT parent_id, id
FROM item_tree
WHERE child_id = id
UNION ALL
SELECT it.parent_id, it.id
FROM item_tree it
JOIN t
ON it.child_id = t.parent_id
)
SELECT id, child_id
FROM t
id
毎回置換するループを外部でプログラムできますが、SQLでのみ実行できますか?
@ダニエル、
元のクエリの出力は次のとおりです。
id parent_id
------ ---------
cornea eye
cornea NULL
cornea head
cornea body
sclera eye
sclera head
sclera NULL
sclera body
tongue body
tongue head
tongue NULL
tongue mouth
tooth body
tooth head
tooth mouth
tooth NULL
ただし、次のように、内部のnullフィルタを削除した場合でも、nullでフィルタリングされたselectステートメントで囲むと、目的の結果が得られます。
select * from (
WITH RECURSIVE t(id,parent_id) AS (
select id,parent_id from item_tree i
UNION ALL
select t.id,i.parent_id from item_tree i JOIN t on i.id=t.parent_id
)
select * from t order by id
) t1 where parent_id is not null;
とにかく、これはバグである可能性があるため、チェックマークをクリックしました(jdbcとpgAdmin3で同じ出力で両方のクエリを実行しようとしました)