gpt4 book ai didi

php - MySQL 使用其他行的数据更改选择输出

转载 作者:行者123 更新时间:2023-11-29 01:42:38 24 4
gpt4 key购买 nike

很抱歉,如果我不知道正确的术语,但我们开始吧:

我有一个包含以下列和行的表格:

------------------------------
| id | parent_id | type |
------------------------------
| 1 | 0 | text1 |
| 2 | 1 | text2 |
------------------------------

现在我想要的是选择类型,但是如果parent_id不为0我希望该类型是其父类型的内容 +[ 自身的类型 ]。

所以在这种情况下,选择的输出将是:

----------------
| type |
----------------
| text1 |
| text1[text2] |
----------------

我正在尝试使用 concat 和 if 语句,但无法解决这个问题。

你能帮帮我吗?

最佳答案

尝试:

select c.id,
case c.parent_id
when 0 then c.type
else concat(p.type,'[',c.type,']')
end as type
from mytable c
left join mytable p on c.parent_id = p.id

SQLFiddle here .

关于php - MySQL 使用其他行的数据更改选择输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15945294/

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