简体   繁体   中英

SQL query help needed to get the data from same table with parent child

I have a table in my oracle db as shown below:

FolderID    Name    ParentFolderID
1200        A       1000
1201        B       1000
1202        C       1000
1203        D       1000
1204        E       1200
1205        F       1200
1206        G       1201
1207        H       1205
1208        I       1205
1209        J       1205

In this table, the folder id 1000 has childs 1200, 1201, 1202, 1203. The folder id 1200 has childs 1204, 1205. 1201 has child 1206. And 1205 has childs 1207, 1208 and 1209. What I am looking for is get all child folder ids for the folder id I am passing to my query. If I pass 1000, it should return all child and sub child rows.

Thanks

select folderid, name
from the_unknown_table
start with folderid = 1000
connect by prior folderid = parentfolderId;

try CONNECT BY query like;

SELECT 
folderid, 
name
FROM the_unknown_table
START WITH  folderid = :first_folder_id
CONNECT BY PRIOR folderid = parentfolderId;

if you want all in one column seperated with commas use SYS_CONNECT_BY_PATH

SELECT 
folderid, 
SYS_CONNECT_BY_PATH(folderid,',') my_path,
name
FROM the_unknown_table
START WITH  folderid = :first_folder_id
CONNECT BY PRIOR folderid = parentfolderId;

The standard SQL way (equivalent to connect by/prior) uses a recursive CTE:

DROP SCHEMA opa CASCADE;
CREATE SCHEMA opa;

SET search_path='opa';

CREATE TABLE parents
    ( id    INTEGER NOT NULL PRIMARY KEY 
    , zname text
    , parent_id INTEGER REFERENCES parents (id)
    );  
INSERT INTO parents(id,zname,parent_id) VALUES
(1000        , '@',       NULL)
,(1200        , 'A',       1000)
,(1201        , 'B',       1000)
,(1202        , 'C',       1000)
,(1203        , 'D',       1000)
,(1204        , 'E',       1200)
,(1205        , 'F',       1200)
,(1206        , 'G',       1201)
,(1207        , 'H',       1205)
,(1208        , 'I',       1205)
,(1209        , 'J',       1205)
    ;   

WITH RECURSIVE zopa AS (
    SELECT p0.id AS opa_id, p0.id AS id
      , p0.zname AS zname, p0.parent_id  AS parent_id
    FROM  parents p0
    WHERE NOT EXISTS (
        SELECT * FROM parents nx WHERE nx.id = p0.parent_id)
    UNION
    SELECT 
        zp.opa_id AS opa_id
        , p1.id AS id
        , p1.zname AS zname
        , p1.parent_id AS parent_id
    FROM parents p1
    JOIN zopa zp ON zp.id = p1.parent_id
    )   
select *
FROM zopa ct
WHERE ct.opa_id = 1000
    ;   

Results:

NOTICE:  drop cascades to table opa.parents
DROP SCHEMA
CREATE SCHEMA
SET
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "parents_pkey" for table "parents"
CREATE TABLE
INSERT 0 11
 opa_id |  id  | zname | parent_id 
--------+------+-------+-----------
   1000 | 1000 | @     |          
   1000 | 1202 | C     |      1000
   1000 | 1200 | A     |      1000
   1000 | 1201 | B     |      1000
   1000 | 1203 | D     |      1000
   1000 | 1205 | F     |      1200
   1000 | 1204 | E     |      1200
   1000 | 1206 | G     |      1201
   1000 | 1209 | J     |      1205
   1000 | 1207 | H     |      1205
   1000 | 1208 | I     |      1205
(11 rows)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM