从父ID中获取所有子用户

时间:2017-01-24 13:32:23

标签: mysql

我正在研究php mlm基础项目,我无法找回所有儿童用户。我正在传递parent_id作为赞助商ID

select * from user where sponserid in(select id from users where sponserid = 1) or sponscerid = 1;

+----+--------+------------+
| id | name   | sponscorid |
+----+--------+------------+
|  1 | one    |      NULL  |
|  2 | two    |         1  |
|  3 | three  |         1  |
|  4 | four   |         2  |
|  5 | five   |         2  |
|  6 | six    |         2  |
|  7 | seven  |         2  |
|  8 | eight  |         3  |
|  9 | nine   |         3  |
| 10 | ten    |         3  |
| 11 | eleven |         3  |
+----+--------+------------+

我正在使用以下查询:

select * from user where sponserid in(select id from users where sponserid = 1) or sponscerid = 1;

但我只能让儿童和孙子女用户达到2级(这意味着我只能获得赞助商= 1& 2的用户)。但我需要父母用户的所有子孙用户。

0 个答案:

没有答案