在2列上使用MYSQL中的MAX

时间:2015-07-07 11:49:26

标签: mysql

我有表odds

  

id | home_team | away_team | event_date | home_price | away_price | bookie | league_id

我试图获得max(home_price)max(away_price)以及每场比赛的赌注:

select max(home_price), max(away_price), bookie 
from odds where league_id=1 
group by home_team, away_team, event_date

我无法为max(home_price)max(away_price)

显示正确的预订

我知道我必须使用"组标识符"但我无法让我工作

我要找的结果是:

  

max(home_price)| bookie_max_home_price | max(away_price | bookie_min_home_price

1 个答案:

答案 0 :(得分:0)

您可以使用相关子查询来获取bookie值:

SELECT home_price,
       (SELECT bookie
        FROM odds AS o1
        WHERE league_id = 1 AND
              o1.home_price = o.home_price AND
              o1.home_team =  o.home_team AND
              o1.away_team =  o.away_team AND
              o1.event_date = o.event_date
        ORDER BY bookie LIMIT 1) AS bookie_home,
       away_price,
       (SELECT bookie
        FROM odds AS o2
        WHERE league_id = 1 AND
              o2.away_price = o.away_price AND
              o2.home_team =  o.home_team AND
              o2.away_team =  o.away_team AND
              o2.event_date = o.event_date
        ORDER BY bookie LIMIT 1) AS bookie_away,
       home_team, away_team, event_date         
FROM (             
  SELECT
    MAX(home_price) AS home_price, 
    MAX(away_price) AS away_price, 
    home_team, 
    away_team, 
    event_date 
  FROM odds  
  WHERE league_id = 1 
  GROUP BY home_team, away_team, event_date ) AS o

Demo here