Mysql 根据包含函数的另一个表中的条件更新表

Mysql 根据包含函数的另一个表中的条件更新表,mysql,select,inner-join,where,Mysql,Select,Inner Join,Where,我有两个mysql表,如下例所示: RaceID CarID Dis Grd Date Time 8 1 200 A 2010-10-10 20.50 8 2 300 A 2010-10-10 30.50 8 3 200 A 2010-10-10 20.10 9 1 200 A 2010-11-10 20.00 12 1 200 A 2011-12-11

我有两个mysql表,如下例所示:

RaceID CarID    Dis Grd Date            Time    
8      1    200 A   2010-10-10  20.50
8      2    300 A   2010-10-10  30.50
8      3    200 A   2010-10-10  20.10
9      1    200 A   2010-11-10  20.00
12     1    200 A   2011-12-11  19.50   
RaceId  CarID   Dis Grd Date        Exp_Time

10  1   200 A   2011-11-11        
10  2   200 A   2011-11-11        
10  3   200 A   2011-11-11        
汽车:

RaceID CarID    Dis Grd Date            Time    
8      1    200 A   2010-10-10  20.50
8      2    300 A   2010-10-10  30.50
8      3    200 A   2010-10-10  20.10
9      1    200 A   2010-11-10  20.00
12     1    200 A   2011-12-11  19.50   
RaceId  CarID   Dis Grd Date        Exp_Time

10  1   200 A   2011-11-11        
10  2   200 A   2011-11-11        
10  3   200 A   2011-11-11        
比赛:

RaceID CarID    Dis Grd Date            Time    
8      1    200 A   2010-10-10  20.50
8      2    300 A   2010-10-10  30.50
8      3    200 A   2010-10-10  20.10
9      1    200 A   2010-11-10  20.00
12     1    200 A   2011-12-11  19.50   
RaceId  CarID   Dis Grd Date        Exp_Time

10  1   200 A   2011-11-11        
10  2   200 A   2011-11-11        
10  3   200 A   2011-11-11        
我想根据CARS表中的数据,在Exp_列时间添加Races表中的数据。 例如:

RaceID CarID    Dis Grd Date            Time    
8      1    200 A   2010-10-10  20.50
8      2    300 A   2010-10-10  30.50
8      3    200 A   2010-10-10  20.10
9      1    200 A   2010-11-10  20.00
12     1    200 A   2011-12-11  19.50   
RaceId  CarID   Dis Grd Date        Exp_Time

10  1   200 A   2011-11-11        
10  2   200 A   2011-11-11        
10  3   200 A   2011-11-11        
RACES.Exp_Time=AVG(CARS.Time) 
WHERE
CARS.CarID=RACES.CarID
CARS.Dis=RACES.Dis
CARS.Grd=RACES.Grd
CARS.Date<RACES.Date
这个想法是,预期的时间是平均从以前的比赛时间相同的距离和grd。未来的比赛应排除在平均计算之外。 问题是从RACES表中获取日期条件。 我做这个查询:

RaceID CarID    Dis Grd Date            Time    
8      1    200 A   2010-10-10  20.50
8      2    300 A   2010-10-10  30.50
8      3    200 A   2010-10-10  20.10
9      1    200 A   2010-11-10  20.00
12     1    200 A   2011-12-11  19.50   
RaceId  CarID   Dis Grd Date        Exp_Time

10  1   200 A   2011-11-11        
10  2   200 A   2011-11-11        
10  3   200 A   2011-11-11        
UPDATE `RACES` c
INNER JOIN (
SELECT CARS.CarID, CARS.Dis, CARS.Grd, CARS.Date, AVG(Time) AS `Exp_Time`
FROM CARS
WHERE CARS.Date<'2011-11-11'
GROUP BY CarID, Dis, Grd
)
x ON c.CarID=x.CarID AND c.Dis=x.Dis AND c.Grd=x.Grd
SET c.Exp_Time=x.Exp_Time
当我输入myseld的日期-2011-11-11时,它就起作用了 我不知道如何从RACES表中获取数据

RaceID CarID    Dis Grd Date            Time    
8      1    200 A   2010-10-10  20.50
8      2    300 A   2010-10-10  30.50
8      3    200 A   2010-10-10  20.10
9      1    200 A   2010-11-10  20.00
12     1    200 A   2011-12-11  19.50   
RaceId  CarID   Dis Grd Date        Exp_Time

10  1   200 A   2011-11-11        
10  2   200 A   2011-11-11        
10  3   200 A   2011-11-11        
有人能帮忙吗? 提前谢谢! 伊万

RaceID CarID    Dis Grd Date            Time    
8      1    200 A   2010-10-10  20.50
8      2    300 A   2010-10-10  30.50
8      3    200 A   2010-10-10  20.10
9      1    200 A   2010-11-10  20.00
12     1    200 A   2011-12-11  19.50   
RaceId  CarID   Dis Grd Date        Exp_Time

10  1   200 A   2011-11-11        
10  2   200 A   2011-11-11        
10  3   200 A   2011-11-11        
2011年11月11日我不知道如何从比赛表中获取数据

RaceID CarID    Dis Grd Date            Time    
8      1    200 A   2010-10-10  20.50
8      2    300 A   2010-10-10  30.50
8      3    200 A   2010-10-10  20.10
9      1    200 A   2010-11-10  20.00
12     1    200 A   2011-12-11  19.50   
RaceId  CarID   Dis Grd Date        Exp_Time

10  1   200 A   2011-11-11        
10  2   200 A   2011-11-11        
10  3   200 A   2011-11-11        
可以将此谓词移动到连接条件,如下所示:

RaceID CarID    Dis Grd Date            Time    
8      1    200 A   2010-10-10  20.50
8      2    300 A   2010-10-10  30.50
8      3    200 A   2010-10-10  20.10
9      1    200 A   2010-11-10  20.00
12     1    200 A   2011-12-11  19.50   
RaceId  CarID   Dis Grd Date        Exp_Time

10  1   200 A   2011-11-11        
10  2   200 A   2011-11-11        
10  3   200 A   2011-11-11        
UPDATE `RACES` c
INNER JOIN (
  SELECT 
     CarID, Dis, Grd, Date, AVG(Time) AS `Exp_Time`
  FROM CARS
  GROUP BY CarID, Dis, Grd
)x  ON c.CarID        = x.CarID 
   AND c.Dis          = x.Dis 
   AND c.Grd          = x.Grd
   AND x.DATE(`Date`) < c.DATE(`Date`)
SET c.Exp_Time = x.Exp_Time;

是的,谢谢。我真的试过了,但看起来我在日期方面遇到了问题,因为它计算不正确。它将车内的所有日期识别为0000-00-00,即使我将公式设置为x.DATEDate>c.datedatedate,它也只返回根据此条件找到的第一个日期,而不是平均值。两个字段类型都设置为date。谢谢,如果我在第二次选择时添加限制1条件,则此操作有效,否则会出现一个错误,即它会发现满足该条件的多个结果。当我在表格比赛中的c.CarID=x.CarID和c.Dis=x.Dis以及c.Grd=x.Grd上得到超过1行满足此条件时,问题就出现了。@user1936814在我的答案中添加限制1,以便对其他人有所帮助