mysql 多层级,MySQL-多级类别结构

Given the following data and table structure:

id name parent_id

================================

1 food NULL

2 snacks 1

3 cakes 2

4 birthdays 3

I would like to output these rows alongside all of their parent categories. So for example I would like to generate a query to output the data as follows:

id name parent_id parent_1 parent_2 parent_3

===================================================================

1 food NULL NULL NULL NULL

2 snacks 1 1 NULL NULL

3 cakes 2 1 2 NULL

4 birthdays 3 1 2 3

With this, I can easily get the IDs of every parent level of a given category.

I have tried doing this using sub queries but not quite managed to get it right. This is what I have tried:

SELECT id, name, parent_id, parent_id AS _parent_1,

(SELECT parent_id FROM categories WHERE id = _parent_1) AS _parent_2,

(SELECT parent_id FROM categories WHERE id = _parent_2) AS _parent_3

FROM `categories`

EDIT: Based on the feedback, it seems it's going to be somewhat difficult to get the data in the desired format.

Would it at the very least be possible to get all child categories of a given category? So for example for category ID 1, it should output the three categories below it (or four if it will include the given category itself).

解决方案

You can simply self-join the table a number of times as required to meet maximum nesting depth. This could look like this:

SELECT

c.id AS id,

c.name AS name,

parent.id AS parent,

grandparent.id AS grandparent,

greatgrandparent.id AS greatgrandparent

/* add other columns based on max depth of nesting */

FROM categories AS c

LEFT JOIN categories AS parent

ON c.parent_id = parent.id

LEFT JOIN categories AS grandparent

ON parent.parent_id = grandparent.id

LEFT JOIN categories AS greatgrandparent

ON grandparent.parent_id = greatgrandparent.id

/* keep joining based on max depth of nesting */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值