需要检索推荐消费最多的前5个推荐人

时间:2015-10-05 23:54:25

标签: php mysql

我需要获得推荐人花费最多的前5名推荐人。我需要得到:

1)总推荐人数

2)仅支付推荐人数

3)支付转介支付的总金额

到目前为止,此查询返回总付费推介,但我也需要完全推荐。它可以在1个查询中完成,还是需要2个?请帮忙!谢谢!

SELECT t1.referrer as referrer, 
       count(t1.username) as total_referrals, 
       count(t2.username) as paying_referrals, 
       SUM(t2.payment) as total_payments
  FROM USER_DATA t1, PAYMENT_INFO t2 
 WHERE t1.username = t2.username  
 GROUP BY referrer
 ORDER BY total_payments DESC 
 LIMIT 0,5

USER_DATA(用户数据,约1000条记录)

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

PAYMENT_INFO(用户付款,约300条记录)

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

0 个答案:

没有答案