如何将查询结果列保存到同一数据库中 table 中的列
How to save query results column into a column in table in same database
我很难将此查询的结果(标题为 "FIPminus" 的列)复制到我在同一数据库中的 table 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'
whosebug.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
这里是table我想复制到的结构的截图:
UPDATE:这是我与产生错误的 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)
据我了解,您已经在 table FIPminus
中填充了您现在需要更新 starting_pitcher_stats
table 的值。
您可以在 UPDATE
命令中使用 JOIN
语法。
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
如果您的查询 运行 对您来说相对较慢,请考虑创建索引 (在 运行 更新之前!) 这将包括用于比较的列tables:
ALTER TABLE FIPminus ADD INDEX (Starting_Pitcher, GAME_ID);
ALTER TABLE starting_pitcher_stats ADD INDEX (Starting_Pitcher, GAME_ID);
您还可以阅读 speed of UPDATE statements 并决定它是否与您相关。
我很难将此查询的结果(标题为 "FIPminus" 的列)复制到我在同一数据库中的 table 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'
whosebug.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
这里是table我想复制到的结构的截图:
UPDATE:这是我与产生错误的 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)
据我了解,您已经在 table FIPminus
中填充了您现在需要更新 starting_pitcher_stats
table 的值。
您可以在 UPDATE
命令中使用 JOIN
语法。
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
如果您的查询 运行 对您来说相对较慢,请考虑创建索引 (在 运行 更新之前!) 这将包括用于比较的列tables:
ALTER TABLE FIPminus ADD INDEX (Starting_Pitcher, GAME_ID);
ALTER TABLE starting_pitcher_stats ADD INDEX (Starting_Pitcher, GAME_ID);
您还可以阅读 speed of UPDATE statements 并决定它是否与您相关。