提供子行时,从同一个表中选择父行

Nor*_*man 3 mysql sql

"id"    "parent"    "name"
"1"     "0"         "Books"
"2"     "1"         "Crime Fiction"
"3"     "2"         "Death On the Nile"
Run Code Online (Sandbox Code Playgroud)

从上面的内容,我如何选择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.
Run Code Online (Sandbox Code Playgroud)

如何在同一张桌子内进行选择?

jue*_*n d 6

select parent.name, child.name
from your_table child
left join your_table parent on child.parent = parent.id
where child.id = 3
Run Code Online (Sandbox Code Playgroud)