这个问题是一个奇怪的问题,我想我只是想着这个错误。我试图(递归)获取与给定承包商关联的所有页面URL。这三个相关的表是:SQL/MySQL递归从同一张表中拉出
|-------------------------------------------------|
| client_sections |
|-------------------------------------------------|
| contractor_id | reusable_section_id |
|-------------------------------------------------|
|--------------------------------------------------------|
| reusable_sections |
|--------------------------------------------------------|
| reusable_category_id | reusable_section_id |
|--------------------------------------------------------|
|--------------------------------------------|
| reusable_content |
|--------------------------------------------|
| reusable_section_id | page_url |
|--------------------------------------------|
现在,这里是需要 - 第一个查询是
SELECT reusable_section_id FROM client_sections where contractor_id = '119'
这得到所有顶级“父母节” - 这是在reusable_category_id
表reusable_sections
。如果我不得不在那里停下来,我会处理它。但是我要注意的是,每个返回的部分“可能”都是父级部分。我试过一个嵌套的SELECT
,结果是没有什么灾难!
什么,我试图:
» Get contractor parent sections by contractor ID from client_sections table
» get sections by reusable_category_id = reusable_section_id in reusable_sections --
» get page_url for all the sections returned (including the initial parents)
by reusable_section_id in reusable_content -- I am good until this point
» recur the last two steps with the current reusable_section_id as
reusable_category_id to check for children (!!#[email protected]#!!!)
» Finally return all pages for all sections under that contractor_id
我试图查询从疯狂JOINS
s到嵌套SELECT
s到无济于事。我读过很多帖子,包括This One。我是一名有能力的SQL开发人员,我认为我的逻辑是有缺陷的,向正确方向发展的一个简单点可能会激发我的创造力! WHILE
循环是最好的方法吗?嵌套0? JOIN
?我的大脑被炸了!帮帮我?
如何递归的多层次,你期待什么呢?如果您没有限制,则仅通过执行左外连接就无法解决此问题。你将需要一个存储过程,否则你将不得不使用编程语言来实现它。它基本上是一棵树。 –
理论上它可能是无限的..这就是为什么递归让我难倒了......我可以用JOIN来完成,否则...... – Zak
https://stackoverflow.com/questions/10646833/using-mysql-query-to-traverse-行 - 制作 - 递归 - 树:D。 看到这个:https://www.slideshare.net/billkarwin/models-for-hierarchical-data –