gpt4 book ai didi

mysql - 提供子行时从同一表中选择父行

转载 作者:可可西里 更新时间:2023-11-01 07:49:44 25 4
gpt4 key购买 nike

"id"    "parent"    "name"
"1" "0" "Books"
"2" "1" "Crime Fiction"
"3" "2" "Death On the Nile"

从类似上面的内容中,我如何选择父行的 name 以及 child 的名称。在这里,将提供子行的名称。我需要获取父级的 name

期望的输出:

@id = 3

Crime Fiction //This is the name of the parent row - in this case 2
Death on the Nile // This is the name of the row who's id was supplied.

如何在同一个表内进行选择?

最佳答案

select parent.name, child.name
from your_table child
left join your_table parent on child.parent = parent.id
where child.id = 3

关于mysql - 提供子行时从同一表中选择父行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19360150/

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