create table folder(
id bigint(20) not null,
parent_id bigint(20) default null,
primary key id
);
建立函式:
create function `getparlist`(rootid bigint)
returns varchar(1000)
begin
declare stemp varchar(1000);
declare stemppar varchar(1000);
set stemp = '';
set stemppar =rootid;
#迴圈遞迴
while stemppar is not null do
#判斷是否是第乙個,不加的話第乙個會為空
if stemp != '' then
set stemp = concat(stemp,',',stemppar);
else
set stemp = stemppar;
end if;
set stemp = concat(stemp,',',stemppar);
select group_concat(parent_id) into stemppar from folder where parent_id<>id and find_in_set(id,stemppar)>0;
end while;
return stemp;
end
呼叫:
select id
from folder
where find_in_set(id,getchildlist(2))
建立函式:
create function `getchildlist`(rootid bigint)
returns varchar(1000)
begin
declare stemp varchar(1000);
declare stempchd varchar(1000);
set stemp = '$';
set stempchd =cast(rootid as char);
while stempchd is not null do
set stemp = concat(stemp,',',stempchd);
select group_concat(id) into stempchd from folder where find_in_set(parent_id,stempchd)>0;
end while;
return stemp;
end
呼叫:
select id
from folder
where find_in_set(id,getparlist(10))
這是我們開啟了bin-log, 我們就必須指定我們的函式是否是
1 deterministic 不確定的
2 no sql 沒有sql語句,當然也不會修改資料
3 reads sql data 只是讀取資料,當然也不會修改資料
4 modifies sql data 要修改資料
5 contains sql 包含了sql語句
其中在function裡面,只有 deterministic, no sql 和 reads sql data 被支援。如果我們開啟了 bin-log, 我們就必須為我們的function指定乙個引數。
在mysql中建立函式時出現這種錯誤的解決方法:
set global log_bin_trust_function_creators=true;
這是我們開啟了bin-log, 我們就必須指定我們的函式是否是
1 deterministic 不確定的
2 no sql 沒有sql語句,當然也不會修改資料
3 reads sql data 只是讀取資料,當然也不會修改資料
4 modifies sql data 要修改資料
5 contains sql 包含了sql語句
其中在function裡面,只有 deterministic, no sql 和 reads sql data 被支援。如果我們開啟了 bin-log, 我們就必須為我們的function指定乙個引數。
在mysql中建立函式時出現這種錯誤的解決方法:
set global log_bin_trust_function_creators=true;
mysql實現父子遞迴查詢sql
在很多業務場景中,我們需要從資料庫查詢一些樹狀結構的資料,多半以id,pid的形式儲存記錄。在oracle中,能夠通過語法輕鬆實現父子級間的遞迴查詢,無論到父,子,孫,曾孫等多少級,都能查出來。但是在mysql中,就沒有像oracle中那樣有現成的語法直接呼叫了。設表test有以下字段 id,nam...
Oracle通過遞迴查詢父子兄弟節點方法示例
前言 說到oracle中的遞迴查詢語法,我覺得有一些資料庫基礎的童鞋應該都知道,做專案的時候應該也會用到,下面本文就來介紹下關於oracle通過遞迴查詢父子兄弟節點的相關內容,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧。方法如下 1 查詢某節點下所有後代節點 包括各級父節點 查詢...
Mysql 遞迴查詢子節點
查詢父編碼及以下所有子節點 select id from select t1.id,t1.parent id,if find in set t1.id,pids 0,pids,if find in set t1.parent id,pids 0,pids concat ws pids,id 0 as...