如何构建一个查询来统计所有在我之后注册的用户?

时间:2017-10-19 01:45:39

标签: php mysql join

我正在使用PHP& amp ;;开发一个MLM应用程序。 MySQL的。我在MySQL查询中遇到了一些困难。

以下是sqlfiddle http://sqlfiddle.com/#!9/0d1b7a/3

的链接

我的表结构是这样的:

enter image description here

我想从表中选择所有记录,并计算成员之后加入的成员数。计数基于created_on字段。我建议的输出如下:

enter image description here

total_under_me的计算错误。

我使用了这个查询:select * from users group by id

2 个答案:

答案 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的用户之后加入的用户数。