關於SQL遞迴查詢在不同資料庫中的實現方法

2021-09-07 03:02:06 字數 3417 閱讀 8267

比如表結構資料如下:

table:tree

id name parentid

1 一級  0

2  二級  1

3  **  2

4 四級  3

sql server 2005查詢方法:

//上查

with tmptreeas(

select * from tree where id=2

union all

select p.* from tmptree inner join tree p on p.id=tmptree.parentid

)select * from tmptree

//下查

with tmptreeas(

select * from tree where id=2

union all

select s.* from tmptree inner join tree s on s.parentid=tmptree.id

)select * from tmptree

sql server 2008及以後版本,還可用如下方法:

增加一列tid,型別設為:hierarchyid(這個是clr型別,表示層級),且取消parentid欄位,變成如下:(表名為:tree2)

tid    id    name

0x      1     一級

0x58     2    二級

0x5b40   3   **

0x5b5e   4   四級

查詢方法:

select *,tid.getlevel() as [level] from tree2 --獲取所有層級

declare @parenttree hierarchyid

select @parenttree=tid from tree2 where id=2

select *,tid.getlevel()as [level] from tree2 where tid.isdescendantof(@parenttree)=1 --獲取指定的節點所有下級

declare @childtree hierarchyid

select @childtree=tid from tree2 where id=3

select *,tid.getlevel()as [level] from tree2 where @childtree.isdescendantof(tid)=1 --獲取指定的節點所有上級

oracle中的查詢方法:

select *

from tree

start with id=2

connect by prior id=parentid --下查

select *

from tree

start with id=2

connect by id= prior parentid --上查

mysql 中的查詢方法:

//定義乙個依據id查詢所有父id為這個指定的id的字串列表,以逗號分隔

create definer=`root`@`localhost` function `getchildlst`(rootid int,direction int) returns varchar(1000) charset utf8

begin

declare stemp varchar(5000);

declare stempchd varchar(1000);

set stemp = '$';

if direction=1 then

set stempchd =cast(rootid as char);

elseif direction=2 then

select cast(parentid as char) into stempchd from tree where id=rootid;

end if;

while stempchd is not null do

set stemp = concat(stemp,',',stempchd);

select group_concat(id) into stempchd from tree where (direction=1 and find_in_set(parentid,stempchd)>0)

or (direction=2 and find_in_set(id,stempchd)>0);

end while;

return stemp;

end//查詢方法:

select * from tree where find_in_set(id,getchildlst(1,1));--下查

select * from tree where find_in_set(id,getchildlst(1,2));--上查

補充說明:上面這個方法在下查是沒有問題,但在上查時會出現問題(詳見博問:),原因在於我的邏輯寫錯了,存在死迴圈,現已修正,新的方法如下:

create definer=`root`@`localhost` function `getchildlst`(rootid int,direction int) returns varchar(1000) charset utf8

begin

declare stemp varchar(5000);

declare stempchd varchar(1000);

set stemp = '$';

set stempchd =cast(rootid as char);

if direction=1 then

while stempchd is not null do

set stemp = concat(stemp,',',stempchd);

select group_concat(id) into stempchd from tree where find_in_set(parentid,stempchd)>0;

end while;

elseif direction=2 then

while stempchd is not null do

set stemp = concat(stemp,',',stempchd);

select group_concat(parentid) into stempchd from tree where find_in_set(id,stempchd)>0;

end while;

end if;

return stemp;

end

這樣遞迴查詢就很方便了。

sql在不同資料庫查詢前幾條資料

sql在不同資料庫查詢前幾條資料 1.oracle select from table1 where rownum n hql from table1 t order by t.createtime desc where rownum n 2.informix select first n from...

sql在不同資料庫查詢前幾條資料

sql在不同資料庫查詢前幾條資料 1.oracle select from table1 where rownum n hql from table1 t order by t.createtime desc where rownum n 2.informix select first n from...

關於資料庫的遞迴查詢

1 oracle 以scott.emp表舉例 empno是人員編號,mgr是上級領導 也就是上級人員編碼 1 從上到下查詢 該查詢查詢員工jones下屬所有的員工 select emp.from emp start with ename jones connect by mgr prior empn...