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 写一个方法checkPerson,入参实体类Person,出参布尔值
- ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
- ¥15 CSAPPattacklab
- ¥15 一直显示正在等待HID—ISP
- ¥15 Python turtle 画图
- ¥15 关于大棚监测的pcb板设计
- ¥15 stm32开发clion时遇到的编译问题
- ¥15 lna设计 源简并电感型共源放大器
- ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)