对不起,标题令人困惑,不知道该如何措辞。

我有一个事件表,以及一个关于这些事件的赌注表。我已经获取了每项赛事的总投注额,但我也想获取赔率(aka,一个团队的总数除以另一团队的总数的商)。下注的参与者存储在horse表的data_bets列中。

例如,我想添加以下内容:

SELECT
    SUM(data_bets.amount) WHERE data_bets.horse = data_events.challenger
    /
    SUM(data_bets.amount) WHERE data_bets.horse = data_events.contestant
AS bet_odds


到我下面的现有查询:

SELECT *,
   SUM(data_bets.amount) AS total_pot,
   COUNT(data_bets.member) AS total_bets,
   COUNT(data_votes.member) AS total_votes,
   data_platforms.name AS platform_name, data_platforms.icon AS platform_icon
FROM data_events
   LEFT JOIN data_bets ON data_bets.event=data_events.id
   LEFT JOIN data_votes ON data_votes.content_type=2 AND data_votes.content_id=data_events.id
   LEFT JOIN data_platforms ON data_platforms.id=data_events.platform
WHERE status=1
GROUP BY data_events.id
ORDER BY total_pot DESC


这可能与一个查询有关吗?如果可能的话,我希望同时获取challenger_oddscontestant_odds,如下所示:

SELECT
    SUM(data_bets.amount) WHERE data_bets.horse = data_events.challenger
    /
    SUM(data_bets.amount) WHERE data_bets.horse = data_events.contestant
AS challenger_odds,
    SUM(data_bets.amount) WHERE data_bets.horse = data_events.contestant
    /
    SUM(data_bets.amount) WHERE data_bets.horse = data_events.challenger
AS contestant_odds

最佳答案

使用条件聚合:

SELECT (SUM(CASE WHEN data_bets.horse = data_events.challenger THEN data_bets.amount ELSE 0 END)  /
        SUM(CASE WHEN data_bets.horse = data_events.contestant THEN data_bets.amount END)
       ) as bet_odds

关于php - 检索两个SUM(col)JOIN不同值的商,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37647666/

10-10 05:39