使用嵌套集模型按深度计算项目

Count of items by depth with a nested set model

我正在为我的数据库使用嵌套集模型(从这里:http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/),它工作得很好,但是...

根据 "Depth of a Sub-Tree" 请求,我想要另一个结果。

初步结果:

+----------------------+-------+
| name                 | depth |
+----------------------+-------+
| PORTABLE ELECTRONICS |     0 |
| MP3 PLAYERS          |     1 |
| FLASH                |     2 |
| CD PLAYERS           |     1 |
| 2 WAY RADIOS         |     1 |
+----------------------+-------+

我想按深度计算项目数,例如初始数据:

+----------------------+-------+
| depth                | count |
+----------------------+-------+
|                    1 |     3 |
|                    2 |     1 |
+----------------------+-------+

我尝试使用 and/or count() 进行分组,但它不起作用...

感谢您的帮助!

这里是草莓评论的答案:

SELECT depth, COUNT(*) FROM (your query here) x GROUP BY depth