mysql多層查詢

2022-04-12 18:21:24 字數 1860 閱讀 9041

附件:city.sql - djmg

mysql查詢某節點下層的所有元素節點:

select parentid, areaid, areacode,areaname, level, center

from city_area

where find_in_set(areaid,

(select group_concat(childrenids) from (

select (

select @childrenids:=group_concat(areaid) from city_area where find_in_set(parentid, @childrenids)) as childrenids

-- ,ceiling( length(@childrenids) / 5 )

from (select @childrenids:=?) var,city_area c where @childrenids is not null

) a )

)-- and areaname like '%區%';

mysql查詢某節點上層的所有元素節點:

select parentid, areaid, areacode,areaname, level, center

from city_area

where find_in_set(areaid,

(select group_concat(childrenids) from (

select (

select @childrenids:=group_concat(parentid) from city_area where find_in_set(areaid, @childrenids)) as childrenids

-- ,ceiling( length(@childrenids) / 5 )

from (select @childrenids:=?) var,city_area c where @childrenids is not null

) a )

)-- and areaname like '%區%';

兩個查詢的不同點在於最內層的查詢將父子id位置對換了一下

(select @childrenids:=group_concat(parentid) from city_area where find_in_set(areaid, @childrenids)) as childrenids

(select @childrenids:=group_concat(areaid) from city_area where find_in_set(parentid, @childrenids)) as childrenids

問題:

group_concat函式被截斷的問題

mysql的 group_concat 函式預設返回1024個位元組長度,超過長度的會被截斷;

命令列下輸入:

set global group_concat_max_len=1024000;

orset session group_concat_max_len=1024000;

jpa原生sql':'轉義問題

字元:在jpa原生sql會被看作是變數的字首

@childrenids:=?==>@childrenids\\:=?

mysql 多層判斷查詢 mysql多層查詢

附件 city.sql djmg mysql查詢某節點下層的所有元素節點 select parentid,areaid,areacode,areaname,level,center from city area where find in set areaid,select group concat...

多層架構通用查詢的思考

一直沒在網上寫自己的心得,又總是看園子很多兄弟的文章。工作幾年了,工作經驗和人生感悟頗多的,中間夾雜著積極與消極 成功與失敗,各種的體會只有自己明白,希望消極的心態不會影響到大家,積極的大家就湊合借鑑和學習一下,嘿嘿。搞了很久的管理系統,能抽象出來的還是很少。最初在表示層做通用查詢的時候,走了很多彎...

mybatise多層集合巢狀查詢 3層

1,用乙個sql,多個resultmap 一般另起乙個子map,不直接在collection中再套 每個子項都需要id才可避免子項自身被覆蓋,永遠1條 例如 id id id select p.id,p.code,p.name,p.long name,p.description,p.type,p.s...