我很难在我在SELECT JOIN语句中创建的表中的列中添加一个总和(好吧,真的有区别)。让我展示我所拥有的(简短形式),然后解释:
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
因此,此表显示了用户的投注历史,在哪个网站,所涉及的球队,投注以及比赛的几率。我通过一个相当长的查询创建了这个表,该查询通过JOIN / ON组合了用户表,投注站点表,游戏表等。
目前,我们的投注站点表中有10个投注站点,我们正在跟踪/每个用户在每个站点上的站点。 所以,在Ridge的个人资料页面上,我还有一张表:
Betting Site Deposit Bonus Rollover Rollover Remaining Days
LeoVegas 4000 4000 10 ? 30
Nordic Bet 4000 4000 6 ? 30
Comeon 3000 3000 12 ? 90
这个问号是我遇到困难的地方。你看,我试图把顶部表中列出的所有赌注的总和与投注站点和用户匹配。通过JOIN语句(和$ _GET变量),我只能选择特定用户和特定投注站点的信息。但我无法让SUM正常工作。这就是我希望它的工作方式:
Rollover Remaining是对特定网站的((奖励*翻转) - 总和(投注))的计算。
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
这就是我的尝试:
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;
但问题是无论投注网站是什么,它都会计算相同的转期金额,因此它们与LeoVegas的顶级投注网站完全相同。有谁知道如何使用SUM来使用$ row中列出的投注站点?我知道(好吧,不知道它会从哪里来)这个错误来自我的SUM WHERE子句,但不知道如何在没有它的情况下总结具体的投注站点。
由于
答案 0 :(得分:0)
我认为您正在为同一网站和ID生成虚拟列,因为
WHERE u.id = $id AND betting_site_name='$betsite') AS 'Total Rollover'
试试以下..
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 inner_ub
JOIN users AS inner_u ON inner_ub.userid = inner_u.id
JOIN bonuses AS inner_b ON inner_ub.siteid = inner_b.id
WHERE inner_u.id = u.id AND inner_b.betting_site_name=b.betting_site_name) 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;