数据库里面一张表存了一棵树,存储结构为菜单栏的1,2,3,4级菜单,以id,par_id
关联,怎么以字段name达到查询出结果一级→二级→三级→四级的形式查询出结果?
select
q.func_id,q.func_name,q.par_func_id,
case
when
exists(select 1 from quota_category c where q.func_id=c.par_func_id)
then
1 else 0 end isParent
from 表名 q
数据库结构如下:
想要的结果:
select
q.func_id,q.func_name,q.par_func_id,
case
when
exists(select 1 from 表名 c where q.func_id=c.par_func_id)
then
1 else 0 end isParent
from 表名 q
尝试用select e.FUNC_ID,e.FUNC_NAME,l.FUNC_NAME from tc_func e LEFT JOIN tc_func l
ON e.PAR_FUNC_ID=l.FUNC_ID
得到两级菜单的结果:
我觉得最好的办法是在业务里面组装成树形数据返回,
具体查看文章:
https://mp.csdn.net/postedit/80812336
个人觉得,这种相对固定,修改率不大的数据,可以先分表查询后在业务层进行封装。
select * from (select col from table) t where t.col='1';