select distinct * from
(select * from news_types where news_types.id=1 union
select n2.* from news_types n1,news_types n2 where n1.id=1 and n2.parentid=n1.id
union select n3.* from news_types n3,(
select n2.* from news_types n1,news_types n2 where n1.id=1 and n2.parentid=n1.id
) t2 where t2.id=n3.parentid) a;
写一个mysql数据库的sql 递归查询,我现在有个能查询三级的,谁能帮我改为查询四级或五级的
写一个mysql数据库的sql 递归查询,我现在有个能查询三级的,谁能帮我改为查询四级或五级的
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答
- ll89308839 2012-03-16 11:06关注
又是你啊,昨天的那个不好用么,那个可是无限层的
按照你写的给你改了个5层的
[code="sql"]
select distinct *
from (select *
from news_types
where news_types.id = 1
union
select n2.*
from news_types n1, news_types n2
where n1.id = 1
and n2.parentid = n1.id
union
select n3.*
from news_types n3,
(select n2.*
from news_types n1, news_types n2
where n1.id = 1
and n2.parentid = n1.id) t2
where t2.id = n3.parentid
union
select n4.*
from news_types n4,
(select n3.*
from news_types n3,
(select n2.*
from news_types n1, news_types n2
where n1.id = 1
and n2.parentid = n1.id) t2
where t2.id = n3.parentid) t3
where t3.id = n4.parentid
union
select n5.*
from news_types n5,
(select n4.*
from news_types n4,
(select n3.*
from news_types n3,
(select n2.*
from news_types n1, news_types n2
where n1.id = 1
and n2.parentid = n1.id) t2
where t2.id = n3.parentid) t3
where t3.id = n4.parentid) t4
where t4.id = n5.parentid
) a;[/code]
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 请教一下各位,为什么我这个没有实现模拟点击
- ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
- ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
- ¥20 有关区间dp的问题求解
- ¥15 多电路系统共用电源的串扰问题
- ¥15 slam rangenet++配置
- ¥15 有没有研究水声通信方面的帮我改俩matlab代码
- ¥15 ubuntu子系统密码忘记
- ¥15 保护模式-系统加载-段寄存器
- ¥15 电脑桌面设定一个区域禁止鼠标操作