mardi 21 juin 2016

PHP MySQL sum column of joined table per row values

I am having a hard time adding a sum (well, really a difference) in a column that is in a table I created in a SELECT JOIN statement. Let me show what I have (short form), and then explain:

User Name   Betting Site    Home Team   Away Team   Wager   Odds
Ridge       LeoVegas        Molde       Tromso      1000    2.20
Ridge       Nordic Bet      Molde       Tromso      3000    1.60
Ridge       LeoVegas        Aalesund    Rosenborg   4000    1.30
Simon       Comeon          Aalesund    Rosenborg   2000    1.40

So this table shows a betting history by a user, on which site, the teams involved, wager and the odds of the game. I had created this table through a fairly long query combining a users table, betting site table, games table, etc through JOIN/ON.

Currently, we have 10 betting sites in our betting site table, and we are keeping track/per user what their standing on each site is. So, on Ridge's profile page, I also have a table:

Betting Site    Deposit     Bonus       Rollover    Rollover Remaining  Days
LeoVegas        4000        4000        10                  ?           30
Nordic Bet      4000        4000        6                   ?           30
Comeon          3000        3000        12                  ?           90

This question mark is where I am having my difficulty. You see, I am trying to take a sum of all the wagers listed in the top table as matched by the betting site and user. Through JOIN statement (and a $_GET variable), I am able to select only information for a specific user and a specific betting site. But I am unable to get the SUM to work correctly. This is how I would like it to work:

The Rollover Remaining is a calculation of the ((bonus * rollover) - sum(wagers)) for a particular site.

Ridge Robinson's Status
Betting Site    Deposit     Bonus       Rollover    Rollover Remaining  Days
LeoVegas        4000        4000        10          35000 (*40000-5000*)30
Nordic Bet      4000        4000        6           21000 (*24000-3000*)30
Comeon          3000        3000        12          36000 (*36000-0*)   90

This is what I have tried:

SELECT
b.betting_site_name,
b.deposit,
b.bonus,
b.odds,
b.rollover,
b.days,
u.wager,
(SELECT
    SUM(wager) AS 'Total Risk'
    FROM userbets AS ub
    JOIN users AS u ON ub.userid = u.id
    JOIN bonuses AS b ON ub.siteid = b.id
    WHERE u.id = $id AND betting_site_name='$betsite') AS 'Total Rollover'
FROM bonuses AS b
LEFT JOIN userbets AS u ON u.siteid = b.id
LEFT JOIN users ON u.userid = users.id
WHERE users.id=$id AND betting_site_name='$betsite'
GROUP BY b.id;

but the problem is that it calculates the same rollover amount no matter what the betting site is, so they are all the same as the top betting site of LeoVegas. Does anyone know how I can use a SUM to use a betting site listed in that $row? I know (well, not sure where else it would be coming from) the mistake is coming from my SUM WHERE clause, but not sure how else to sum the specific betting site without that.

Thanks

Aucun commentaire:

Enregistrer un commentaire