--根據指定節點ID獲取全部子節點-- WITH TEMP AS ( SELECT * FROM table_name WHERE Id='4' --表的主鍵ID UNION ALL SELECT T0.* FROM TEMP,table_name T0 WHERE TEMP.Id=T0.ParentId --子級ID==父級ID ) SELECT * FROM TEMP; --根據指定節點ID獲取全部父節點-- WITH TEMP AS ( SELECT * FROM table_name WHERE Id='32' --表的主鍵ID UNION ALL SELECT T0.* FROM TEMP,table_name T0 WHERE TEMP.ParentId=T0.Id --父級ID==子級ID ) SELECT * FROM TEMP;