项目里想查询某个部门下的所有部门,表结构大体为这样主要字段depid部门编号,depname部门名称,updepid上级部门编号,根据updepid来关联。本来想利用程序写个递归的方法查询出来,后来上网一查还有Sql语句可以直接都查询出来的方法,记录下。
with temp as (select * from sys_dep where manager='' union all
select t.* from temp,sys_dep t where temp.depid = t.updepid)
select * from temp