MySQL与Sql Server数据库的区别——递归查询父子关系

1、问题:现有树形的菜单表数据(树形列表,如下,第1张图是sql server数据库,第2张mysql数据库),需要找出指定菜单id的节点以及它的子节点

2、Sql实现:

(1)Sql Server数据库的递归查询父子关系的Sql语句的写法如下:

1  with temp  as 
2  ( 
3     select id  from SysMenu where id = '55E0BE65-2EC7-4ADC-B14B-B88293B2B6CA'
4     union all 
5     select a.id  from SysMenu a 
6     inner join temp on a.parentId = temp.id 
7 ) 
8 select * from temp
View Code

运行结果:

 

 (2)MySQL数据库的递归查询父子关系的Sql语句的写法如下:

1 with RECURSIVE temp  as 
2 ( 
3         select `id`  from SysMenu a where `id` = '57282330-4e39-45fa-888b-d50dd5c86646'
4         union all 
5         select a.`id`  from SysMenu a,temp t where t.`id` = a.parentId
6 ) 
7 select * from temp
View Code

运行结果:

 

posted @ 2023-06-14 18:55  逆风起降  阅读(33)  评论(0编辑  收藏  举报