利用 oracle 项目一样寻常利用,start with ... connect by 语法做菜单栏数据查询,该语法在人大金仓数据库提供的可视化工具中可以执行,但在Springboot + mybatis 项目中无法执行(版本2.X),通过与人大金仓人员谈判,可以利用 with recursive.... as(... union ..) select ... where... 语法替代,该语法基础是如下:
假设有如下一张表(menu_table):
id
menu_id
parent_menu_id
menu_desc
1
1
一级
2
101
1
二级
3
10101
101
三级
4
1010101
10101
四级
我们可以利用一下 SQL查询出树状数据:
with recursive temp as(
select * from menu_table where id = 3
union
select * from menu_table a join temp b on b.parent_menu_id = a.menu_id
) select * from temp
该SQL执行结果如下:
id
menu_id
parent_menu_id
menu_desc
1
1
一级
2
101
1
二级
3
10101
101
三级
SQL 语法分析:
SQL 中的 select * from menu_table where id = 3 查出来的数据,会作为初始基础数据,union 后面的SQL select * from menu_table a join temp b on b.parent_menu_id = a.menu_id , 界说树状数据的规则,当前SQL是向上查询,利用基础数据中 parent_menu_id 值 101 为条件去 menu_table 表中查询 menu_id 值为 101 的数据,会递归处理直到不满足该条件为止 b.parent_menu_id = a.menu_id
如果我们要向下查找数据要怎么做呢?把条件该为如许 b.menu_id = a.parent_menu_id
2.数据精度问题