獲取當前用戶的所有上級【SQL-所有上級】

Q:如何獲取當前用戶的上級、上級的上級、上級的上級的上級............

A:具體您可以參考以下說明:
在oracle下,使用start with  connect by prior即可
  1. select * from t_user start with id = '11e1-7f98-dc0c84d6-a04d-05b43c063ac2' connect by prior id = superior
複製代碼
----------------------------------------------------------------------------
在sqlserver下,只能使用存儲過程完成,大致代碼見下:
  1. with cte as
  2. (
  3.     select *, 0 as lvl from t_user
  4.     where Id = '11e0-4b86-4e2e6951-a06c-ade030ea555d'
  5.     union all
  6.     select d.*,lvl + 1 from cte c inner join t_user d
  7.     on c.Id = d.superior
  8. )
  9. select * from cte
複製代碼
----------------------------------------------------------------------------
在最新版本的mysql下,只能使用存儲過程以及遞歸完成,大致可以參考以下鏈接:
http://blog.csdn.net/ACMAIN_CHM/article/details/4142971

ps:Myapps打包出去的綠色版的版本是5.1.37,還不支持以上的解決方案

----------------------------------------------------------------------------

如果希望通過使用腳本的方式來獲取可以參考下帖:
http://www.teemlink.com/bbs/viewthread.php?tid=166074
發佈了160 篇原創文章 · 獲贊 3 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章