sql 遞迴查詢 孩子節點資料

2021-10-04 08:29:55 字數 1035 閱讀 1153

# sql 遞迴查詢 孩子節點資料

-- 向上查詢(查詢所有父親節點,包含自己)

with

recursive res as

(select t1.

*from gaia_ocm_area as t1

--where t1.id =

'當前節點'

union

select t2.

*from gaia_ocm_area as t2

inner

join res as t3 on t2.id = t3.parent_id

where1=

1)select res.

*from res

-- 向下查詢(查詢所有孩子節點,包含自己)

with

recursive res as

(select t1.

*from gaia_ocm_area as t1

--where t1.id =

'當前節點'

union

select t2.

*from gaia_ocm_area as t2

inner

join res as t3 on t3.id = t2.parent_id

where1=

1)select res.

*from res

-- 向下查詢(查詢所有兒子節點,不包含自己) --

>

with

recursive res as

(select t1.

*from gaia_ocm_area as t1

where t1.parent_id = #

union

select t2.

*from gaia_ocm_area as t2

inner

join res as t3 on t3.id = t2.parent_id

)select res.

*from res

SQL臨時表查詢所有子節點資料

方法一 create table 臨時表名 欄位1 約束條件,欄位2 約束條件,方法二 select into 臨時表名 from 你的表 方法三 利用with語句 sql server 2005 版本 with test id,name as select bucode,bunamech from...

sql遞迴查詢父節點的例子

由於專案要分地區,而且是自己定義的 所有建立乙個分組表 create table group info group id bigint 10 not null auto increment comment 分組id content varchar 2000 default null comment ...

父節點遞迴查詢和子節點遞迴查詢函式

由於在專案中用到了向上查詢父節點的遞迴查詢和向下查詢子節點查詢的遞迴查詢,由於在實現過程中,寫遞迴查詢的sql函式 花費了較長的時間,所以在此總結一下,兩種遞迴查詢的函式的實現寫法 向下查詢子節點的遞迴查詢 先展示表結構 根據區域查詢對應的下級區域資訊,其中parentid為zoneid對應的下級區...