遞歸查詢sql
SQL 資料庫 實現遞歸查詢的幾種代碼方法 表結構
ProctCategory
CategoryID Level ParentCategoryID
數據
T SQL
WITH CategoryTemp(CategoryID ParentCategoryID) 臨時表用來保存查到的Category
(
SELECT CategoryID ParentCategoryID FROM ProctCategory WHERE ParentCategoryID<= 將所有的第一層查出來作為初始數據 需要查第幾層或者哪個ParentCategoryID下面所有的 N層 把ParentCategoryID賦相關的值即可
UNION ALL 查詢N層
SELECT pc CategoryID ParentCategoryID FROM ProctCategory pc
LEFT JOIN CategoryTemp ct ON pc ParentCategoryID=ct CategoryID
WHERE ParentCategoryID> 因為第一層前面已經查出來了 所以這里把第一層篩選掉
)
SELECT CategoryID ParentCategoryID FROM CategoryTemp
結果
裂鍵檔
如果把ParentCategoryID賦為 結果則為
實例
ID是否為部門 部門名 上級ID y 部門 y 部門 n 張三 n 李二 y 部門 n 王五 y 部門3亮賀 n 小三 我想找詢 ID 值為 下級的所有人員包括下級部門的所有人員
創建查詢函數 create function f_id( @id int 要查詢的id )returns @re table(id int level int) as begin declare @l int set @l= insert @re select id @l from 表 where 上級id=@id while @@rowcount> begin set @l=@l+ insert @re select a id @l from 表 a join @re b on a 上級id=b id and b level=@l end return end go
調用函數進行查詢 select a * from 表 a join f_id( ) b on a id=b id
聯合查詢
測試數據 create table 表(ID int 是否為部門 char( ) 部門名 varchar( ) 上級ID int) insert 表 select y 部門 union all select y 部門 union all肆亂 select n 張三 union all select n 李二 union all select y 部門 union all select n 王五 union all select y 部門 union all select n 小三 go
創建查詢函數 create function f_id( @id int 要查詢的id )returns @re table(id int level int) as begin declare @l int set @l= insert @re select id @l from 表 where 上級id=@id while @@rowcount> begin set @l=@l+ insert @re select a id @l from 表 a join @re b on a 上級id=b id and b level=@l end return end go
調用函數進行查詢 select a * from 表 a join f_id( ) b on a id=b id go
刪除測試 drop table 表 drop function f_id
/* 測試結果
ID 是否為部門 部門名 上級ID n 小三
lishixin/Article/program/MySQL/201311/29557
② SQL遞歸查詢所有子節點
你這樣設計的表用遞歸來顯示最恰當 我剛剛好有這個的代碼 發給你參考下吧
/// <summary>
/// 綁定根節點
/// </summary>
/// <param name="id"></param>
/// <param name="ddlList"></param>
public void BindSysMenu(string id, DropDownList ddlList)
{
ListItem ll = new ListItem();
ll.Text = "╋--請選擇-----";
ll.Value = "-1";
ddlList.Items.Add(ll);
DataTable dt = new ManageContentInfoBll().GetWName(id).Tables[0];
foreach (DataRow dr in dt.Rows)
{
ListItem li = new ListItem();
li.Text = "╋" + dr["W_Name"].ToString();
li.Value = dr["W_ID"].ToString();
ddlList.Items.Add(li);
BindSysMenuChild((dr["W_ID"].ToString()), ddlList, "├—");
}
}
/// <summary>
/// 綁定子節點
/// </summary>
public void BindSysMenuChild(string id, DropDownList ddlList, String separtor)
{
DataTable dt = new ManageContentInfoBll().GetWName(id).Tables[0];
foreach (DataRow dr in dt.Rows)
{
ListItem li = new ListItem();
li.Text = separtor + dr["W_Name"].ToString();
li.Value = dr["W_ID"].ToString();
string separtor_ = separtor + "——";
ddlList.Items.Add(li);
BindSysMenuChild(dr["W_ID"].ToString(), ddlList, separtor_);
}
}
sql 語句 很簡單的
select * from WebSubjectMenu where W_Logo ="+id+"
W_Logo 是上級的ID
希望對你有幫助吧。
③ sql語句實現遞歸查詢所有節點,mysql和oracle都能用的
首先說一下Oracle的遞歸查詢,相信大部分人都知道很簡單。無非start with connect by 函數。下面是從pId向子節點遞歸查詢的例子,unId是資料庫表中的主鍵。
如果是從子節點遞歸到父節點查詢,就把start with 換成unid,prior左右對換
下面再講MySql 的遞歸查詢方式。MySql沒有Oracle的強大功能,雖然都是同一個公司的產品。所以只能靠自己寫。有很多方法,用sql去循環查詢,或者寫存儲過程,我這里只提供一種。就是新建一個function函數。
表結構不說了,無非就是 Id ,pId,其他列。下面是創建一個遞歸查詢子節點的函數
DROP FUNCTION IF EXISTS queryChildrenPowerInfo;
CREATE FUNCTION `queryChildrenPowerInfo` (powerId VARCHAR(2000))
RETURNS VARCHAR(2000)
BEGIN
DECLARE sTemp VARCHAR(2000);
DECLARE sTempChd VARCHAR(2000);
SET sTemp = '$';
SET sTempChd = cast(powerId as CHAR);
WHILE sTempChd is not NULL DO
SET sTemp = CONCAT(sTemp, ',', sTempChd);
SELECT group_concat(id) INTO sTempChd FROM t_discretionary_power where FIND_IN_SET(pId,sTempChd)>0;
END WHILE;
return sTemp;
調用的時候:select queryChildrenPowerInfo(""); 該語句會返回Id和父Id等於傳入參數powerId的一個字元串,中間有逗號隔開如圖
下面這句代碼的意思是,查詢出 t_discretionary_power 表中,t.id 等於上面查詢出的結果集的數據。FIND_IN_SET(A,B)是MYSQL的函數。意思是查找在B集合中有A的數據。相當於In
select t.* from t_discretionary_power t where FIND_IN_SET(t.id,queryChildrenPowerInfo(''))
④ sql 怎麼遞歸查詢的方法:
1.創建測試表,createtabletest_connect(idnumber,p_idnumber);