在postgresql中,我有下表
c_relation_investor_main_pfk; c_relation_investor_sub_pfk
"inv_ho";"inv_ho_r"
"inv_ho";"inv_ho_sf"
"inv_priv";"inv_com"
"inv_priv";"inv_encor"
"inv_priv";"inv_ho"
"inv_prof";"inv_b4"
"inv_prof";"inv_com"
"inv_prof";"inv_encor"
"inv_prof";"inv_ho"
"inv_prof";"inv_pd"
"inv_prof";"inv_ut"
考虑到孩子的名字,我怎样才能列出所有的名人?并且给定父名称,如何列出其所有后代?
答案 0 :(得分:0)
我发布了解决方案,因此它可以帮助遇到类似问题的人。
WITH RECURSIVE source(main ,sub ) AS (
SELECT character varying 'inv_ho', character varying 'inv_ho_r'
UNION ALL
SELECT tb_relation_investor.c_relation_investor_main_pfk, tb_relation_investor.c_relation_investor_sub_pfk
FROM source JOIN tb_relation_investor ON (source.main=tb_relation_investor.c_relation_investor_sub_pfk)
)
SELECT * from source