Search code examples
mysqlcalculated-columnssequelpro

How to save query results column into a column in table in same database


I'm having difficulty copying the results of this query (a column titled "FIPminus") into a table starting_pitcher_stats that I have in the same database.

When I run the following code, the results appear as in the screenshot beneath the code below. However, when I use an UPDATE statement below the ALTER TABLE statement, as discussed in the following thread, I keep getting the following error:

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

Here is a screenshot of the structure of the table I'd like to copy into:

enter image description here

UPDATE: Here is the code that I used with the UPDATE statement that produced the error:

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

Thank you in advance.

UPDATE: Here is the code that worked, but it took ridiculously long to run:

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)

Solution

  • From what I understand you already populated your table FIPminus with values that you now need to update starting_pitcher_stats table with.

    You can use JOIN syntax in UPDATE command.

    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
    

    If your query is running relatively slow for you, consider creating indexes (before running update !) which would include columns used in comparison on both tables:

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

    You could also read about speed of UPDATE statements and decide whether or not it is relevant for you.