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...

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bed6a7-31b7f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bed6a7-31b7f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?