我现在正在使用以下查询...
SELECT id, account_number,
(SELECT count(*)
FROM `member_tbl` t2
WHERE t2.referred_by = t1.account_number
) AS total_referred
FROM `member_tbl` t1;
这提供了以下结果......
id - id of memenr
account_number - account number of user
total_referred - total number of members referred by account_number
但是我们希望获得更多结果,例如以下...
total_reffered2 - total number of members that THEY(all accounts referred by account_number) all referred
total_reffered3 - total number of members that referred by all members of total_reffered2
请检查mysql小提琴
并且根据mysql fiddle data,查询必须返回以下值...
| id | account_number | referred_by1 | referred_by2 | referred_by3
+----+-----------------+---------------+--------------+-------------
| 1 | ac203003 | 0 | 0 | 0
+----+-----------------+---------------+--------------+-------------
| 2 | ac203004 | 1 | 1 | 0
+----+-----------------+---------------+--------------+-------------
| 3 | ac203005 | 2 | 1 | 1
+----+-----------------+---------------+--------------+-------------
让我再举一个例子
如果每个成员总是有 10 次推荐,那么...
referred_by1 = 10
referred_by2 = 100
referred_by3 = 1000
我很困惑,我该如何写这个查询?