具有完整树父项的MySql邻接列表

时间:2014-04-12 08:02:59

标签: mysql sql tree adjacency-list adjacency-list-model

我希望在本教程之后使用MySql中的邻接列表模型: 带有查询的Managing Hierarchical Data in MySQL

SELECT t1.name AS lev1, t2.name as lev2, t3.name as lev3, t4.name as lev4
FROM category AS t1
LEFT JOIN category AS t2 ON t2.parent = t1.category_id
LEFT JOIN category AS t3 ON t3.parent = t2.category_id
LEFT JOIN category AS t4 ON t4.parent = t3.category_id
WHERE t1.name = 'ELECTRONICS';

我的问题是,即如果查询返回:

 | ELECTRONICS | TELEVISIONS | TUBE | NULL |
 | ELECTRONICS | TELEVISIONS | LCD | NULL |
 | ELECTRONICS | TELEVISIONS | PLASMA | NULL |

如何修改查询以获取此结果数据+ Leaf节点ID(category_id)的附加列?

ID | ELECTRONICS | NULL |NULL | NULL |
ID | ELECTRONICS | TELEVISIONS |NULL | NULL |
ID | ELECTRONICS | TELEVISIONS |TUBE | NULL |
  

1 .ID =电子的身份

     

2 .ID = TELEVISIONS的ID

     

3 .ID = TUBE的ID

我想要显示完整列表树和独立父母,以及他们的ID ...

0 个答案:

没有答案