简体   繁体   中英

Count of items by depth with a nested set model

I'm using the nested set model for my database (from here: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ ), it works perfectly, but...

From the "Depth of a Sub-Tree" request, I want an other result.

Initial result:

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

And I want the count of items by depth, example from the initial data:

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

I tried to work with group by and/or count(), but it doesn't work...

Thanks for your help!

这是草莓评论的答案:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM