PostgreSQL with recursion

Source: Internet
Author: User
Tags postgresql

In PostgreSQL, the WITH clause provides a way to write the auxiliary reports and queries used in a large query. It helps break down complex and large queries in a simple and easy-to-read form.

1. Build a table
[SQL]View PlainCopy
    1. postgres=# Create table tb9 (ID serial primary key,name character varying, parentid   Integer);
    2. CREATE TABLE
[SQL]View PlainCopy
  1. postgres=# \d TB9
  2. Table "public.tb9"
  3. Column |                    Type | Modifiers
  4. ----------+-------------------+--------------------------------------------------
  5. ID | Integer | Not null default Nextval (' tb9_id_seq ':: regclass)
  6. name |   Character Varying |
  7. ParentID |   Integer |
  8. Indexes:
  9. "Tb9_pkey" PRIMARY KEY, btree (ID)
2. Inserting test data
[SQL]View PlainCopy
  1. postgres=# INSERT INTO tb9 values (generate_series (1,5),' John ', 0);
  2. INSERT 0 5
  3. postgres=# INSERT INTO tb9 values (6,' john1 ', 1);
  4. INSERT 0 1
  5. postgres=# INSERT INTO tb9 values (7,' John2 ', 1);
  6. INSERT 0 1
  7. postgres=# INSERT INTO tb9 values (8,' John11 ', 6);
  8. INSERT 0 1
[SQL]View PlainCopy
  1. postgres=# SELECT * from tb9;
  2. ID | name | ParentID
  3. ----+--------+----------
  4. 1 |        John | 0
  5. 2 |        John | 0
  6. 3 |        John | 0
  7. 4 |        John | 0
  8. 5 |        John | 0
  9. 6 |        john1 | 1
  10. 7 |        john2 | 1
  11. 8 |        John11 | 6
  12. (8 rows)
3. With clause
[SQL]View PlainCopy
    1. postgres=# with t as (select * from tb9 where parentid=1) select count (0) from t;
    2. Count
    3. -------
    4. 2
    5. (1 row)
[SQL]View PlainCopy
    1. postgres=# with t (a,b,c) as (select * from tb9 where parentid=1) select a,b,c from t;
    2. A | B | C
    3. ---+-------+---
    4. 6 | john1 | 1
    5. 7 | john2 | 1
    6. (2 rows)
4. Combined use of multiple with clauses
Parentid=1 all child records of the record
[SQL]View PlainCopy
    1. postgres=# with t1 as  (select * from tb9), T2  as (select * from tb9  where parentid=1)  select t1.* from t1,t2 WHERE T2.ID=T1.PARENTID;  
    2.  id |  name  | parentid   
    3. - ---+--------+----------  
    4.   8 | john11 |         6  
    5. (1 row)   
5. Recursion
All child records for records with ID 1
[SQL]View PlainCopy
  1. postgres=# with recursive T as (select ID,name,parentid from tb9 where id=1 UNION ALL select k.id,k. name,k.parentid from tb9 k,t where T.id=k.parentid) select * from t;
  2. ID | name | ParentID
  3. ----+--------+----------
  4. 1 |        John | 0
  5. 6 |        john1 | 1
  6. 7 |        john2 | 1
  7. 8 |        John11 | 6
  8. 9 |        John21 | 7
  9. (5 rows)


Transfer from http://blog.csdn.net/luojinbai/article/details/44015581

PostgreSQL with recursion

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.