無限層級結構的table1表,Id(主鍵),ParentId(父級id)查詢某個Id的全部下級或全部上級,使用WITH AS,UNION ALL 查詢
spa
一、查詢Id爲1全部的下級code
WITH T AS( SELECT * FROM table1 WHERE Id=1 UNION ALL SELECT a.* FROM table1 a INNER JOIN T ON a.ParentId=T.Id ) SELECT * FROM T
二、查詢Id爲88全部的上級blog
WITH T AS( SELECT * FROM table1 WHERE Id=88 UNION ALL SELECT a.* FROM table1 a INNER JOIN T ON a.Id=T.ParentId ) SELECT * FROM T