【发布时间】:2014-08-05 10:49:29
【问题描述】:
我有一个包含 3 行的表,例如 cat_id、cat_name、cat_parent。
我的桌子看起来像:
-------------------------------------
|cat_id | cat_name | cat_parent
-------------------------------------
| 1 | Electronics | 0
| 2 | mobile | 1
| 3 | ac | 1
| 4 | Furniture | 0
| 5 | Chair | 4
我想使用 MySql 单查询选择父类别及其子类别。
有什么帮助吗?
【问题讨论】:
-
你有什么尝试
-
使用
self join点击此链接http://www.tutorialspoint.com/sql/sql-self-joins.htm了解 -
子表和父表之间的独特之处
-
已知或未知的深度/儿童数量?
-
非常感谢@Rahul 和Agha Umair Ahmed。 :)