本文介绍了MySQL的,加入同一张桌子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张看起来像的桌子

I have a table that looks like

我想进行查询,以便以类似以下的行结尾

I want to do a query so that I can end with with rows that looks like

基本上就是这样

SELECT id, (SELECT name FROM pages WHERE id=parentId), name FROM pages

显然,查询是可怕的.

推荐答案

您可以尝试以下操作,它仅限于两个级别的父级和子级.它不会为3个或更多的水平,例如.父母->孩子->孩子

You could try something like the following, it is restricted to two levels Parent and child.It wont work for 3 or more levels eg. Parent->Child->Child

SELECT  Parent.ID,
        Child.name,
        Parent.Name
FRom    yourTable as Parent LEFT JOIN
        yourTable as Child On Parent.ParentID = Child.ID

这篇关于MySQL的,加入同一张桌子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 06:03