MySQL - 如何访问表中第2级的所有节点

时间:2015-01-19 07:49:20

标签: php database categorization

我正在为具有嵌套类别的电子商务网站创建数据库,我正在使用修改前序遍历算法。我的问题是如何访问2级中的所有节点,即文章投资组合联系

enter image description here

1 个答案:

答案 0 :(得分:3)

The article没有明确告诉您如何从一个级别获取所有节点。但如果你仔细阅读它会告诉你如何做更多 - >获取每个类别的深度计数。然后,您所要做的就是按该深度进行过滤。

SELECT node.name, (COUNT(parent.name) - 1) AS depth
FROM nested_category AS node, nested_category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
GROUP BY node.name
HAVING depth = 1
ORDER BY node.lft;

编辑(发生了什么):

为了使用lft表的rgtnested_category列,我们应该选择两次表。

SELECT *
FROM nested_category AS node, nested_category AS parent

如果您检查此查询,您会发现nested_category中的每一行都会再次选择所有行。所以我们现在想要的是删除第一个表(我们称之为AS node)的所有行,它们不是parent的孩子。这就是我们使用WHERE node.lft BETWEEN parent.lft AND parent.rgt

进行过滤的原因

我想提一下这个查询:

SELECT *
FROM nested_category AS node, nested_category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
ORDER BY node.lft;

等于

SELECT *
FROM nested_category AS node
LEFT JOIN nested_category AS parent ON (node.lft BETWEEN parent.lft AND parent.rgt)
ORDER BY node.lft;

所以现在我们所有的孩子都和他们的父母一起加1(因为我们过滤的方式,每个child属于自己)

+-------------+----------------------+-----+-----+-------------+----------------------+------+------+
| category_id | name                 | lft | rgt | category_id | name                 | lft  | rgt  |
+-------------+----------------------+-----+-----+-------------+----------------------+------+------+
|           1 | ELECTRONICS          |   1 |  20 |           1 | ELECTRONICS          |    1 |   20 |
|           2 | TELEVISIONS          |   2 |   9 |           1 | ELECTRONICS          |    1 |   20 |
|           2 | TELEVISIONS          |   2 |   9 |           2 | TELEVISIONS          |    2 |    9 |
|           3 | TUBE                 |   3 |   4 |           1 | ELECTRONICS          |    1 |   20 |
|           3 | TUBE                 |   3 |   4 |           3 | TUBE                 |    3 |    4 |
|           3 | TUBE                 |   3 |   4 |           2 | TELEVISIONS          |    2 |    9 |
|           4 | LCD                  |   5 |   6 |           2 | TELEVISIONS          |    2 |    9 |
|           4 | LCD                  |   5 |   6 |           1 | ELECTRONICS          |    1 |   20 |
|           4 | LCD                  |   5 |   6 |           4 | LCD                  |    5 |    6 |
|           5 | PLASMA               |   7 |   8 |           1 | ELECTRONICS          |    1 |   20 |
|           5 | PLASMA               |   7 |   8 |           5 | PLASMA               |    7 |    8 |
|           5 | PLASMA               |   7 |   8 |           2 | TELEVISIONS          |    2 |    9 |
|           6 | PORTABLE ELECTRONICS |  10 |  19 |           6 | PORTABLE ELECTRONICS |   10 |   19 |
|           6 | PORTABLE ELECTRONICS |  10 |  19 |           1 | ELECTRONICS          |    1 |   20 |
|           7 | MP3 PLAYERS          |  11 |  14 |           7 | MP3 PLAYERS          |   11 |   14 |
|           7 | MP3 PLAYERS          |  11 |  14 |           1 | ELECTRONICS          |    1 |   20 |
|           7 | MP3 PLAYERS          |  11 |  14 |           6 | PORTABLE ELECTRONICS |   10 |   19 |
|           8 | FLASH                |  12 |  13 |           1 | ELECTRONICS          |    1 |   20 |
|           8 | FLASH                |  12 |  13 |           8 | FLASH                |   12 |   13 |
|           8 | FLASH                |  12 |  13 |           6 | PORTABLE ELECTRONICS |   10 |   19 |
|           8 | FLASH                |  12 |  13 |           7 | MP3 PLAYERS          |   11 |   14 |
|           9 | CD PLAYERS           |  15 |  16 |           1 | ELECTRONICS          |    1 |   20 |
|           9 | CD PLAYERS           |  15 |  16 |           9 | CD PLAYERS           |   15 |   16 |
|           9 | CD PLAYERS           |  15 |  16 |           6 | PORTABLE ELECTRONICS |   10 |   19 |
|          10 | 2 WAY RADIOS         |  17 |  18 |           1 | ELECTRONICS          |    1 |   20 |
|          10 | 2 WAY RADIOS         |  17 |  18 |          10 | 2 WAY RADIOS         |   17 |   18 |
|          10 | 2 WAY RADIOS         |  17 |  18 |           6 | PORTABLE ELECTRONICS |   10 |   19 |
+-------------+----------------------+-----+-----+-------------+----------------------+------+------+

下一步 - 获取深度计数。为了做到这一点,我们必须按每个孩子进行分组(该示例使用GROUP BY node.name,但也可以在node.category_id上进行分组,并计算每个群组parents - 1的数量{ {1}}(也可以改为使用(COUNT(parent.name) - 1) AS depth

这样做

parent.category_id

我们得到了这个

SELECT node.*, (COUNT(parent.category_id) - 1) AS depth
FROM nested_category AS node
LEFT JOIN nested_category AS parent ON (node.lft BETWEEN parent.lft AND parent.rgt)
GROUP BY node.category_id
ORDER BY node.lft;

现在是最后一步,我们只想要这些记录,这些记录的深度= 1(+-------------+----------------------+-----+-----+-------+ | category_id | name | lft | rgt | depth | +-------------+----------------------+-----+-----+-------+ | 1 | ELECTRONICS | 1 | 20 | 0 | | 2 | TELEVISIONS | 2 | 9 | 1 | | 3 | TUBE | 3 | 4 | 2 | | 4 | LCD | 5 | 6 | 2 | | 5 | PLASMA | 7 | 8 | 2 | | 6 | PORTABLE ELECTRONICS | 10 | 19 | 1 | | 7 | MP3 PLAYERS | 11 | 14 | 2 | | 8 | FLASH | 12 | 13 | 3 | | 9 | CD PLAYERS | 15 | 16 | 2 | | 10 | 2 WAY RADIOS | 17 | 18 | 2 | +-------------+----------------------+-----+-----+-------+ HAVING depth = 1在这里使用,因为它是在聚合后应用的(因此它可以过滤聚合))

HAVING

我希望现在更加明确。如果我犯了一些错误,再次抱歉我的英语不好。