我想在本教程之后在 MySql 中使用邻接列表模型: Managing Hierarchical Data in MySQL with query:
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 |
如何修改查询以获取此结果数据 + 叶节点 ID (category_id) 的附加列?
ID | ELECTRONICS | NULL |NULL | NULL |
ID | ELECTRONICS | TELEVISIONS |NULL | NULL |
ID | ELECTRONICS | TELEVISIONS |TUBE | NULL |
1 .ID = ELECTRONICS 的 ID
2 .ID = 电视的 ID
3 .ID = TUBE 的 ID
我想要显示完整的列表树和独立的父母,以及他们的 ID...