gpt4 book ai didi

mysql - 如果在mysql中有父id,如何选择具有相同值的两列并忽略列

转载 作者:行者123 更新时间:2023-11-29 06:40:01 25 4
gpt4 key购买 nike

我的表结构是(抱歉我的英语不好):

enter image description here

我想创建类别和类别下。例如,如果 record 有一个父 id ,这意味着类别在类别之下。我想要如果任何记录有父 ID,则显示正确。

我测试这个:

select tb1.id,tb1.catname,tb2.id,tb2.catname from adsystem.cat as tb1 inner join adsystem.cat as tb2 on tb1.id=tb2.parentid

但输出是:

enter image description here

和测试:

select tb1.id,tb1.catname,tb2.id,tb2.catname from adsystem.cat as tb1 left join adsystem.cat as tb2 on tb1.id=tb2.parentid

这几乎是:看

enter image description here我不想用红线显示猫:

enter image description here

如果输出正确,我可以用 php 来修正

我想完全像这样输出(这只是 photoshop):

enter image description here

最佳答案

我想这个查询是必需的:

select tb1.id, 
case when tb2.catname is null
then tb1.catname
else tb2.catname
end catname,
tb1.parentid
from adsystem.cat as tb1
left join adsystem.cat as tb2 on tb1.parentid = tb2.id

也许我明白了...

select tb1.id cat_id, tb1.catname cat, 
tb2.id subcat_id, tb2.catname subcat from
(select distinct id, catname
from adsystem.cat
where parentid is null or parentid = '') tb1
left join adsystem.cat as tb2 on tb2.parentid = tb1.id
;

关于mysql - 如果在mysql中有父id,如何选择具有相同值的两列并忽略列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22149359/

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