php - MySQL: Get the TOP 3 referrers which referrals spend the most money -


i need top 3 referrers referrals spend money. need number of referrals , total amount referrals have spent. possible done in 1 query? far have query not sure can missing. please help! thank you!

select sum(t2.payment) total_payments, t2.username referral, t1.referrer referrer t1, t2  t1.username = t2.username   group referrer order total_payments desc  limit 0,10 

t1 (user data, ~1000 records)

username   referrer  user1       user67 user2       user89 user3       user89   user4       user57 user5       user57 user6       user89 user7       user67 user8       user89 ... 

t2 (user payments, ~300 records)

username    payment user1        100 user1        200 user2        350 user1         50 user5        500 user1        200 user3        150 user2         80 ... 

below query should give correct result :

 select t1.referrer referrer, count(t2.username) referral, sum(t2.payment) total_payments t1, t2  t1.username = t2.username   group referrer order total_payments desc  limit 0,10    

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -