如何将查询结果列保存到同一数据库中的表中的列中

时间:2016-02-19 22:24:24

标签: mysql calculated-columns sequelpro

我无法将此查询的结果(标题为“FIPminus”的列)复制到我在同一数据库中的表starting_pitcher_stats中。

当我运行以下代码时,结果显示在下面代码下方的屏幕截图中。但是,当我在ALTER TABLE语句下面使用UPDATE语句时,如以下线程所述,我不断收到以下错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT 
IF(s.HomeAway_CD ='Home'

stackoverflow.com/questions/13317838/how-to-insert-a-query-result-into-a-column-mysql

ALTER TABLE starting_pitcher_stats ADD COLUMN FIPminus DOUBLE;
    SELECT 
    IF(s.HomeAway_CD ='Home'
    ,((((13*s.HR_a)+(3*(s.walks_a+s.HBP))-(2*s.strike_outs))/s.innings_pitched + g.cFIP + (((13*s.HR_a)+(3*(s.walks_a+s.HBP))-(2*s.strike_outs))/s.innings_pitched + g.cFIP - (((13*s.HR_a)+(3*(s.walks_a+s.HBP))-(2*s.strike_outs))/s.innings_pitched + g.cFIP * (p.Basic_PF/100))))/(b.FIP)*100)
    ,(((13*s.HR_a)+(3*(s.walks_a+s.HBP))-(2*s.strike_outs))/s.innings_pitched + g.cFIP/(b.FIP)*100)) AS FIPminus
    FROM starting_pitcher_game_log AS s
    INNER JOIN GUTS AS g
       ON s.YEAR_ID=g.YEAR_ID  
    INNER JOIN Park_Factors AS p
       ON s.YEAR_ID=p.YEAR_ID AND s.FLD_TEAM_ID=p.Team_ID
    INNER JOIN AL_NL_MLB_lg_avg_pitcher_stats AS b
       ON s.YEAR_ID=b.YEAR_ID AND s.lg_ID=b.lg_ID WHERE b.pitcher_type='starter'
    GROUP BY s.Starting_Pitcher, s.GAME_ID

enter image description here

以下是我要复制到的表格结构的屏幕截图:

enter image description here

更新:这是我用UPDATE语句生成错误的代码:

ALTER TABLE starting_pitcher_stats ADD COLUMN FIPminus DOUBLE;
UPDATE starting_pitcher_stats
SELECT 
IF(s.HomeAway_CD ='Home'
,((((13*s.HR_a)+(3*(s.walks_a+s.HBP))-(2*s.strike_outs))/s.innings_pitched + g.cFIP + (((13*s.HR_a)+(3*(s.walks_a+s.HBP))-(2*s.strike_outs))/s.innings_pitched + g.cFIP - (((13*s.HR_a)+(3*(s.walks_a+s.HBP))-(2*s.strike_outs))/s.innings_pitched + g.cFIP * (p.Basic_PF/100))))/(b.FIP)*100)
,(((13*s.HR_a)+(3*(s.walks_a+s.HBP))-(2*s.strike_outs))/s.innings_pitched + g.cFIP/(b.FIP)*100)) AS FIPminus
FROM starting_pitcher_game_log AS s
INNER JOIN GUTS AS g
   ON s.YEAR_ID=g.YEAR_ID  
INNER JOIN Park_Factors AS p
   ON s.YEAR_ID=p.YEAR_ID AND s.FLD_TEAM_ID=p.Team_ID
INNER JOIN AL_NL_MLB_lg_avg_pitcher_stats AS b
   ON s.YEAR_ID=b.YEAR_ID AND s.lg_ID=b.lg_ID WHERE b.pitcher_type='starter'
GROUP BY s.Starting_Pitcher, s.GAME_ID

提前谢谢。

更新: 这是有效的代码,但运行起来却花了很长时间:

ALTER TABLE starting_pitcher_stats ADD COLUMN FIPminus DOUBLE;
UPDATE starting_pitcher_stats AS s
SET FIPminus = (SELECT f.FIPminus FROM FIPminus AS f
WHERE s.Starting_Pitcher=f.Starting_Pitcher AND s.GAME_ID=f.GAME_ID)

1 个答案:

答案 0 :(得分:1)

根据我的理解,您已使用现在需要更新FIPminus表格的值填充了表格starting_pitcher_stats

您可以在JOIN命令中使用UPDATE语法。

UPDATE 
  starting_pitcher_stats AS s
  INNER JOIN FIPminus AS f ON 
    s.Starting_Pitcher = f.Starting_Pitcher AND s.GAME_ID = f.GAME_ID
SET FIPminus = f.FIPminus

如果您的查询运行速度相对较慢,请考虑创建索引(在运行更新之前!),其中包括在两个表上进行比较时使用的列:

ALTER TABLE FIPminus ADD INDEX (Starting_Pitcher, GAME_ID);
ALTER TABLE starting_pitcher_stats ADD INDEX (Starting_Pitcher, GAME_ID);

您还可以阅读speed of UPDATE statements并确定它是否与您相关。