MySQL中遞迴查詢

2021-10-01 06:53:35 字數 792 閱讀 7185

今天在業務中,需要對sql ,進行修改,之前的sql 是跑在oracle中,現在跑在mysql上

1. 在oracle中sql 語句如下

select uuid, substr(xs,2,length(xs)) as ztc, mc,xspy,parentid from oa_ztc where (xs like '%工業%' or xspy like '%工業%')  start with uuid in (select uuid from oa_ztc where parentid ='1' )connect by prior   uuid =  parentid
2 . 在mysql中為了實現同樣的功能

with recursive cte as (

select uuid, substr(xs,2,length(xs)) as ztc, mc,xspy,parentid from oa_ztc where xs like '%工業%' or xspy like '%工業%' and uuid in (select uuid from oa_ztc where parentid ='1')

union all

select a.uuid, substr(a.xs,2,length(a.xs)) as ztc, a.mc,a.xspy,a.parentid from oa_ztc a,cte c where c.uuid = a.parentid

) select uuid,ztc, mc,xspy,parentid from cte

Mysql中的遞迴查詢

需求 按條件查詢選單及其子選單。表結構sys menu表,一列是id,一列是parent id,level表示選單層級,name選單名稱。其中level 1的跟選單,parent id為空。查詢語句select pid,menu.from select from sys menu order by ...

mysql 遞迴 mysql遞迴查詢

find in set 函式 函式語法 find in set str,strlist str 代表要查詢的字串 strlist 是乙個以逗號分隔的字串,如 a,b,c 此函式用於查詢 str 字串在字串 strlist 中的位置,返回結果為 1 n 若沒有找到,則返回0。concat 它用於連線n...

mysql遞迴查詢統計 mysql遞迴查詢

樣例資料 create table treenodes id int primary key,nodename varchar 20 pid int select from treenodes id nodename pid 1 a 0 2 b 1 3 c 1 4 d 2 5 e 2 6 f 3 7...