我在我的数据库中使用嵌套集模型(从这里:http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/),它完美无缺,但是...
来自子树的深度"请求,我想要另一个结果。
初步结果:
+----------------------+-------+
| name | depth |
+----------------------+-------+
| PORTABLE ELECTRONICS | 0 |
| MP3 PLAYERS | 1 |
| FLASH | 2 |
| CD PLAYERS | 1 |
| 2 WAY RADIOS | 1 |
+----------------------+-------+
我想按深度计算项目数,例如初始数据:
+----------------------+-------+
| depth | count |
+----------------------+-------+
| 1 | 3 |
| 2 | 1 |
+----------------------+-------+
我尝试使用group by和/或count(),但它没有工作......
感谢您的帮助!
答案 0 :(得分:0)
这是草莓评论的答案:
SELECT depth, COUNT(*) FROM (your query here) x GROUP BY depth