我正在使用PHP& amp ;;开发一个MLM应用程序。 MySQL的。我在MySQL查询中遇到了一些困难。
以下是sqlfiddle http://sqlfiddle.com/#!9/0d1b7a/3
的链接我的表结构是这样的:
我想从表中选择所有记录,并计算成员之后加入的成员数。计数基于created_on
字段。我建议的输出如下:
total_under_me
的计算错误。
我使用了这个查询:select * from users group by id
答案 0 :(得分:1)
我认为你可以使用这样的子查询:
select t1.*, (select count(t2.`id`) from `users` as t2 where t2.`created_on` > t1.`created_on`) as `after_this` from users as t1
答案 1 :(得分:0)
我这样做的方法是计算created_on
大于有问题用户的created_on
值的用户总数。
SELECT COUNT(*) as total_under_me from users
WHERE created_on >
(SELECT created_on FROM users WHERE first_name ='Admin')
在上面的示例中,我计算的是在用户名为Admin的用户之后加入的用户数。