当前位置: 首页 > 数据库 > MSSQL > 正文

Sql Server中如何通过父记录查找出所有关联的子记录

时间:2015-02-26

可查4级Sql代码

select menu_id,menu_name,Father_Id from Sys_Menu where menu_id='18' or Father_Id=18 or -- 一级、二级  
Father_Id in (select menu_id from Sys_Menu a where  menu_id='18' or Father_Id=18) or   -- 三级  
father_id in (select menu_id from sys_menu where Father_Id in (select menu_id from Sys_Menu a where  menu_id='18' or Father_Id=18)) -- 四级

select menu_id,menu_name,Father_Id from Sys_Menu where menu_id='18' or Father_Id=18 or -- 一级、二级

Father_Id in (select menu_id from Sys_Menu a where  menu_id='18' or Father_Id=18) or   -- 三级

father_id in (select menu_id from sys_menu where Father_Id in (select menu_id from Sys_Menu a where  menu_id='18' or Father_Id=18)) -- 四级

更多精彩内容:http://www.bianceng.cn/database/SQLServer/