我有一个新想法,我认为这将是很好的。其思路是这样的:在category_parent列中,我们将插入对此节点的所有父节点的引用。
+----+----------------------+-----------------+
| id | category_name | hierarchy |
+----+----------------------+-----------------+
| 1 | cat1 | 1 |
+----+----------------------+-----------------+
| 2 | cat2 | 2 |
+----+----------------------+-----------------+
| 3 | cat3 | 3 |
+----+----------------------+-----------------+
| 4 | subcat1_1 | 1-4 |
+----+----------------------+-----------------+
| 5 | subcat1_2 | 1-5 |
+----+----------------------+-----------------+
| 6 | subsubcat1_1 | 1-4-6 |
+----+----------------------+-----------------+
| 7 | subsubcat1_2 | 1-4-7 |
+----+----------------------+-----------------+
| 8 | subsubcat1_3 | 1-4-8 |
+----+----------------------+-----------------+
| 9 | subsubcat1_3_1 | 1-4-8-9 |
+----+----------------------+-----------------+
| 10 | subsubcat1_3_2 | 1-4-8-10 |
+----+----------------------+-----------------+
| 11 | subsubcat1_3_1_1 | 1-4-8-9-11 |
+----+----------------------+-----------------+
| 12 | subsubsubcat1_3_1_1 | 1-4-8-9-12 |
+----+----------------------+-----------------+
| 13 | subsubsubcat1_3_1_2 | 1-4-8-9-11-13 |
+----+----------------------+-----------------+
| 14 | subsubsubcat1_2_1_3 | 1-4-8-9-11-14 |
+----+----------------------+-----------------+
如果你看一下我更新的表格,你会注意到每条记录都有一个链接到它的父级,不仅是直接的,还有所有父级。对于这项工作,我做了一些修改,插入到:
Insert into table_name (category_name, hierarchy) values ('new_name', (concat(parent_hierarch, '-', (SELECT Auto_increment FROM information_schema.tables WHERE table_name='table_name'))))
现在,让我们进行所需的查询:
1-汽车的所有子类别:
select * from table_name where hierarchy like '1-%'
2-如果您需要BLACK的所有父级,您只需键入:
select * from table_name where hierarchy = '1-4-8-9' or hierarchy = '1-4-8' or hierarchy = '1-4' or hierarchy = '1'
(您可以从php构建该查询,在'-'char处拆分层次结构字段)
3-查看所有类别,包括级别和直接父级:
select *, SUBSTR(hierarchy, 1, (LENGTH(hierarchy) - LENGTH(id) - 1)) as parent, LENGTH(hierarchy) - LENGTH(REPLACE(hierarchy, '-', '')) as level From table_name
+----+----------------------+-----------------+-----------+--------+
| id | category name | hierarchy | parent | level |
+----+----------------------+-----------------+-----------+--------+
| 1 | cat1 | 1 | | 0 |
+----+----------------------+-----------------+-----------+--------+
| 2 | cat2 | 2 | | 0 |
+----+----------------------+-----------------+-----------+--------+
| 3 | cat3 | 3 | | 0 |
+----+----------------------+-----------------+-----------+--------+
| 4 | subcat1_1 | 1-4 | 1 | 1 |
+----+----------------------+-----------------+-----------+--------+
| 5 | subcat1_2 | 1-5 | 1 | 1 |
+----+----------------------+-----------------+-----------+--------+
| 6 | subsubcat1_1 | 1-4-6 | 1-4 | 2 |
+----+----------------------+-----------------+-----------+--------+
| 7 | subsubcat1_2 | 1-4-7 | 1-4 | 2 |
+----+----------------------+-----------------+-----------+--------+
| 8 | subsubcat1_3 | 1-4-8 | 1-4 | 2 |
+----+----------------------+-----------------+-----------+--------+
| 9 | subsubcat1_3_1 | 1-4-8-9 | 1-4-8 | 3 |
+----+----------------------+-----------------+-----------+--------+
| 10 | subsubcat1_3_2 | 1-4-8-10 | 1-4-8 | 3 |
+----+----------------------+-----------------+-----------+--------+
| 11 | subsubcat1_3_1_1 | 1-4-8-9-11 | 1-4-8-9 | 4 |
+----+----------------------+-----------------+-----------+--------+
| 12 | subsubsubcat1_3_1_1 | 1-4-8-9-12 | 1-4-8-9 | 4 |
+----+----------------------+-----------------+-----------+--------+
| 13 | subsubsubcat1_3_1_2 | 1-4-8-9-11-13 |1-4-8-9-11 | 5 |
+----+----------------------+-----------------+-----------+--------+
| 14 | subsubsubcat1_2_1_3 | 1-4-8-9-11-14 |1-4-8-9-11 | 5 |
+----+----------------------+-----------------+-----------+--------+
这是一个新想法,需要一些改进。