mysql 資料表父子關聯資料查詢sql

2021-10-14 15:33:34 字數 1105 閱讀 7653

mysql資料庫表person_organization中,主鍵是organization_id,另有一列parent_organization_id指向父機構的資料記錄;通過以下sql可以取出指定organization_id所在的樹狀結構枝幹上所有的organization_id:

(

-- 查詢所有子孫organization_id

select organization_id from (

select t1.organization_id,

if (find_in_set(parent_organization_id, @pids) > 0, @pids := concat(@pids, ',', organization_id), 0) as ischild

from

(select organization_id, parent_organization_id from person_organization t where t.status = '0' order by parent_organization_id, organization_id, type) t1,

(select @pids := #) t2

) t3 where ischild != '0'

)uinion

(-- 查詢所有父祖organization_id

select t2.organization_id from (

select @r as _id,

(select @r := parent_organization_id from person_organization

where organization_id = _id) as parent_organization_id, @l := @l + 1 as lvl from

(select @r := #, @l := '') vars, person_organization h where @r ]]> '' and h.status = '0') t1 join person_organization t2 on t1._id = t2.organization_id order by t1.lvl asc

)

Hive外部表 內部表關聯資料

create table page view viewtime int,userid bigint,page url string,referrer url string,ip string comment ip address of the user comment this is the pag...

mysql根據查詢條件更新資料表

menu templateitem表資料更新 插入郵箱伺服器的修改列929 insert ignore into menu templateitem menu template,menu id select menu template,929 from menu templateitem where...

yii資料表關聯操作

首先在model中進行表關聯 public function relations 在增加和修改的頁面用下面的 來表示 textfield model s addon telephone array name hactiveform name model,addon telephone pattern...