t_girl=#CREATE TEMPORARY TABLE tmp_country_relation asSELECT b.id,a.name,b.parentid,''::text as parentname FROM country AS a,country_relation AS b WHERE a.id = b.id;
SELECT 16
Time: 11.773 ms
t_girl=#
这里更新了对应的ID为NAME。
t_girl=#update tmp_country_relation set parentname = a.name from country as a where parentid = a.id;
UPDATE 15
Time: 1.829 ms
我用TABLEFUNC扩展带来的CONNECT BY 实现这棵树的遍历。
t_girl=#select path from connectby('tmp_country_relation as a','a.name','a.parentname','Earth',0,'/')as g(id text,parentid text,level int,path text)orderbylevel;