Sql tree structure queries the number of child nodes of each parent node

now there is a tree structure:
id pid name
10 qq
21 ww
3 1 ee
the result I need is
10 qq 2
21 ww 0
31 ee 0
query all nodes and the number of children of the corresponding nodes. How should I write sql?

Sql
Jul.12,2022

Tree-structured storage in the form of parent-child nodes is not very suitable for this kind of sql
you can refer to my article: https://codeshelper.com/a/11...

Menu