PostgreSQL traversal of a simple tree

Source: Internet
Author: User

Yesterday I used MySQL to implement the Oracle recursive statement connect by, which looks slightly more complicated. See how PostgreSQL is implementing connect by for Oracle today.
or using the same table and data as yesterday. PostgreSQL prides itself on being the most Oracle-like database, so most of the statements can be implemented in a simple and disguised way.
At this point, you can use his own with recursive functionality, and you can also use a third-party extension like the Connect by function.


First Look at the 1th, with a recursive with to show the path of this tree.


t_girl=# with recursive tmp_country (Id,path) as t_girl-# (T_girl (# Select a.ID, '/' | | B.name as "path" from Country_relation as a inner join country as B in (a.id = b.id) where A.parentid is Nullt_girl (# Uni On Allt_girl (# Select A.id,q.path| | ' /' | |  B.name as "path" from Country_relation as a inner joins Tmp_country as q on (q.id = A.parentid) t_girl (# INNER JOIN country                     As B on (a.id = b.id) t_girl (#) t_girl-# Select A.path from Tmp_country as A;  Path-----------------------------------------------/earth/earth/north America/earth/south America /earth/europe/earth/asia/earth/africa/earth/australia/earth/north America/canada/earth/north America/Central America/earth/north america/island Nations/earth/north america/united states/earth/north America/United States/ Alabama/earth/north america/united States/alaska/earth/north america/united States/arizona/earth/north America/ States/arkansas/earth/north america/united States/california (rows) time:3.260 ms 




You can also use the Connect by function with the tablefunc extension to traverse the tree.
Since the two tables designed yesterday are related by ID, this extension comes with the function to show the name is more troublesome, simply I used a temporary table to save the results I want.


t_girl=# CREATE Temporary TABLE tmp_country_relation  as SELECT B.id,a.name,b.parentid, ":: Text as ParentName from Country as a,country_relation as b WHERE a.id = b.id;      




The corresponding ID is updated here for name.
t_girl=# Update tmp_country_relation Set parentname = A.name from country as a where parentid = a.id; UPDATE 15time:1.829 ms




I use the Connect by Tablefunc extension to implement this tree traversal.
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) order by level;                        Path                     


PostgreSQL traversal of a simple tree

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.