我有一张这样的table:
+----+----------+----------+-----------+
| ID | Name | Is_Group | Parent_id |
+----+----------+----------+-----------+
| 1 | Clothes | 1 | Null |
| 2 | Food | 1 | Null |
| 3 | fastfood | 1 | 2 |
| 4 | T-shirt | 0 | 1 |
| 5 | skirt | 0 | 1 |
| 6 | pizza | 0 | 3 |
| 7 | snack | 0 | 3 |
+----+----------+----------+-----------+
我希望有一个横向代表用于报告,例如:
+----+---------+---------+----------+
| ID | Name | level1 | level2 |
+----+---------+---------+----------+
| 4 | T-shirt | Clothes | Null |
| 5 | skirt | Clothes | Null |
| 6 | pizza | Food | fastfood |
| 7 | snack | Food | fastfood |
+----+---------+---------+----------+
有人知道怎么做吗?
1条答案
按热度按时间4sup72z81#
您可以使用两个级别的
left join
: