【发布时间】:2014-01-18 13:56:31
【问题描述】:
我有一张这样的桌子:
--------------------Categories-------------------
| Category | Label | Parent |
*************************************************
| / | Home | NULL |
-------------------------------------------------
| leasing | Leasing | / |
-------------------------------------------------
| leasing-steps | Leasing Steps | leasing |
-------------------------------------------------
| new-tenant | New Tenants | leasing-steps |
-------------------------------------------------
| properties | Properties | leasing |
-------------------------------------------------
| about | About Us | / |
-------------------------------------------------
我从 PHP 收到一个变量(类别),假设值为'leasing-steps'。
我需要提出一个 SQL 查询,该查询将能够采用任何类别并以正确的顺序给我它的层次结构。对于此示例 ('leasing-steps'),它必须输出以下内容:
---Categories---
| Category |
*****************
| / |
-----------------
| leasing |
-----------------
| leasing-steps |
-----------------
我试过这个:
SELECT `Cat1`.`Category`
FROM Categories AS `Cat1`
LEFT JOIN Categories AS `Cat2` ON `Cat1`.`Parent`
WHERE `Cat1`.`Category` = 'leasing-steps'
但它只给了我 1 个条目,而不是我想要的结果。
请注意,我正在与数量不详的父母一起工作。它可以是 1 级或 99999
【问题讨论】:
-
看到这个答案 - stackoverflow.com/a/11035966/689579
-
这可以通过一个 php 循环来获取父/类别并使用新值查询它直到你得到所有但最好修改你的数据库模型或添加存储过程/过程.