gpt4 book ai didi

mysql - 如何显示 child 的 child ?

转载 作者:行者123 更新时间:2023-11-29 13:19:18 26 4
gpt4 key购买 nike

我有一个名为内容的表,我在其中添加顶级页面和子页面。顶级页面将使用 parent__id 作为页面标题主页的 id,子页面将具有各自的 parent__id。当我添加页面时,它看起来像这样。

id parent__id title and so on.
1 0 Home
2 1 About
3 2 services
4 1 contact us

这里我想显示它像

Home
>About
>>services
>contact

我已经尝试过了

<cfquery name="d" datasource="mydata">
SELECT p.id as parentid,
p.title as parent,
p.nav_depth,
c.id as childid,
c.title as child,
c.parent__id as child_parentid
from content p
left join content as c on c.parent__id = p.id
where p.site__id = "8432381492061036983"
group by p.id, c.id
order by p.id, c.id, c.nav_order
</cfquery>
<cfdump var="#d#">

<cfoutput query="d" group="parentid">
<cfif d.nav_depth EQ 0>
#d.parent#<br />
<cfif d.parentid EQ d.child_parentid>
<cfoutput>> #d.child#<br /></cfoutput>
</cfif>
</cfif>
<cfif d.nav_depth GT 0>
<cfif d.childid EQ d.child_parentid>
<cfoutput> >> #d.child<br /></cfoutput>
</cfif>
</cfif>
</cfoutput>

我得到这样的输出。当我查看转储时,它显示正确的结果。

Home
> About US
> Services

任何人都可以帮我解决在查询或 cfoutput 中应该做什么来以不同方式标记子项的子项并将其显示在父项下方吗?这是我完整的表结构与数据 http://sqlfiddle.com/#!2/a29fc/1/0 。谢谢

最佳答案

我不知道 child 来自哪里,但您需要的是嵌套组输出。这应该会让你走上正轨。最值得注意的是,您的查询是按照它们在屏幕上显示的顺序排序的。您当前的 SQL Fiddle 没有正确执行该部分。

<cfoutput query="d" group="parentid">
#d.parent#<br />
<cfoutput group="child"><!--- group based on child --->
> #d.child#<br /><!--- output child header --->
<cfoutput>
>> #d.child<br /><!--- all matching children for current d.child --->
</cfoutput>
</cfoutput>
</cfoutput>

这将输出

Home
> About Us
>> Our Works
> Contact Us

关于mysql - 如何显示 child 的 child ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21062750/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com