简体   繁体   中英

MySQL View table for referrals?

I have a table (user) with the following info:

id  |  user_id |  name    |  country    | referral_id
----------------------------------------------------
1   |  10      |  Jhon    |  India      | 0
2   |  11      |  Krish   |  America    | 0
3   |  12      |  Boss    |  Canada     | 0
4   |  13      |  Jack    |  India      | 11
5   |  14      |  Ronald  |  Japan      | 10
6   |  15      |  Jey     |  Germany    | 10

And have other table (total_earning) with following info:

id  |  user_id  |  date          | earning
--------------------------------------------
1   |  10       |  2015-10-25    | 4.4$
2   |  14       |  2015-10-25    | 2.2$
3   |  15       |  2015-10-27    | 3.0$
4   |  15       |  2015-10-25    | 1.5$

I Want to give Referral Payment (10% of that user earning) to users.

eg. User.10 has 2 referrals ( User.14 and User.15 ), so i want give 10% from user.14 & user.15 earning.

How can I create a MySQL View table from this 2 table?

You need a two-part query. The first query is on a per-user basis of ANY possible earnings. This is the "JustMe" alias... what did EACH person earn directly.

From that, doing a LEFT-JOIN (as not all users had referrals from other) to the second query "Referrals" alias. This joins the earnings to the users table and grabs the referral Id from the USERS table as the grouping. You could have 10 people all be referral from user X, so you want ALL their earnings rolled-up into one for user X.

From that, then JOIN to the user table to pull the who the main person was with their earnings PLUS a column to show the total referral bonus they would receive.

select
      U.Name,
      U.Country,
      JustMe.JustMyEarnings,
      coalesce( Referrals.ReferralEarnings * .1, 0 ) as ReferralBonus
from
   ( select
           te.user_id,
           sum( te.earning ) justMyEarnings
        from
           total_earning te
        group by
           te.user_id ) JustMe
   LEFT JOIN
   ( select
           U2.referral_id,
           sum( te2.earning ) as ReferralEarnings
        from
           total_earnings te2
              join users U2
                 on te2.user_id = U2.user_id
                AND U2.referral_id > 0
        group by
           U2.referral_id ) Referrals
      on JustMe.user_id = Referrals.referral_id
   JOIN Users U
      on JustMe.user_id = U.user_id

AND, if you want this as a view, just

CREATE VIEW UserReferralView as
(entire select from above...)

BUT, if you want the results for a single user, then I would adjust as you DO NOT want the inner queries to query the entire table every time you want totals for one person. I would adjust the inner queries to something like

   ( select
           te.user_id,
           sum( te.earning ) justMyEarnings
        from
           total_earning te
        where
           te.user_id = THE_ONE_USERID_YOU_WANT
        group by
           te.user_id ) JustMe
   LEFT JOIN
   ( select
           U2.referral_id,
           sum( te2.earning ) as ReferralEarnings
        from
           total_earnings te2
              join users U2
                 on te2.user_id = U2.user_id
                AND U2.referral_id > 0
                AND te2.referral_id = THE_ONE_USERID_YOU_WANT
        group by
           U2.referral_id ) Referrals

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM