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

2021-10-19 20:37:32 字數 1629 閱讀 7987

附件: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;

or set session group_concat_max_len=1024000;

jpa原生sql':'轉義問題

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

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

mysql多層查詢

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

mysql 查詢判斷空值

select a.return car area,case when a.return car area then 0 else 1 end from t base bq branch a select a.return car area,case a.return car area when th...

如何優雅的寫多層if判斷

前言 在日常專案中,我們經常會使用到if來進行判斷,可是多層的時候,你就會發現 很累贅 需求現在有4個產品,分別是手機 電腦 電視機 遊戲機,當然每個產品顯示的 不一樣 if判斷 看到這樣的需求,第一時間寫個if判斷,簡單快速,我們來看看 let commodity if commodity.pho...