Try something like this, I changed the subquery table names to make it more clear:
Select
user.id,
all_time.total AS theCount,
last_month.total AS theCount2
From users AS user
Left Outer Join
(Select Count(id) as total, invited_by_id
From users
Group By invited_by_id) as all_time
On all_time.invited_by_id = user.id
Left Outer Join
(Select Count(id) as total, invited_by_id
From users
Where signup_date >= NOW() - INTERVAL 30 DAY
Group By invited_by_id) AS last_month
On last_month.invited_by_id = user.id
If you work often, be sure to get user.invited_by_idindexed!
source
share