I've got MLM structure with multiple childs in X-level (not binary tree marketing) So table consist: parentid and managerid (child). It looks like on this picture:
As you can see, Manager 1 is the main manager with parentid=0 Every manager can have child from 0 to XXX For Manager 1, Manager X is in his 4th level (levels are not limited)
So the question is: "How can I coun all managerid for exact parent id"? i.e. for manager 2 (who has 3 levels and 6 managers in it) or manager 6 (who has 2 leves and 5 managers in it).
Database: MySql
CREATE TABLE `accounts` (
`id` int(11) NOT NULL,
`managerid` varchar(32) COLLATE utf8_unicode_ci DEFAULT '',
`parentid` varchar(32) COLLATE utf8_unicode_ci DEFAULT '',
`name` varchar(256) COLLATE utf8_unicode_ci DEFAULT '',
`regdate` date DEFAULT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
EDIT: So I'm looking for query that will return all managerid for an parentid (without using mysql stored procedures). i.e. simple INSERT
id,managerid,parentid,name,regdate
1,1000,0,name1,2018-12-10
2,1001,1000,name2,2018-12-10
3,1002,1001,name3,2018-12-10
4,1003,1002,name4,2018-12-10
5,1004,1003,name5,2018-12-10
6,1005,1004,name6,2018-12-10
7,1006,1005,name7,2018-12-10
8,1007,1006,name8,2018-12-10
9,1008,1007,name9,2018-12-10
10,1009,1008,name10,2018-12-10
11,1010,1009,name11,2018-12-10
12,1011,1010,name12,2018-12-10
13,1012,1011,name13,2018-12-10
14,1013,1012,name14,2018-12-10
15,1014,1013,name15,2018-12-10
16,1015,1014,name16,2018-12-10
17,1016,1015,name17,2018-12-10
18,1017,1016,name18,2018-12-10
19,1018,1017,name19,2018-12-10
So in this example manager=1000 has 18 childs manager 1005 has 13 childs
I need to enter parentid=1005 (for manager 1005) count all his childs and get number = 13 Don't forget that parentid=1005 may have childs on different levels (from 1 to xxx infinity). I need to count all his childs on all his levels (see attached image above, for "manager 2")