gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:42:38 24 4
gpt4 key购买 nike

我无法将此查询的结果(标题为“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)

最佳答案

据我了解,您已经在表 FIPminus 中填充了您现在需要更新 starting_pitcher_stats 表的值。

您可以在 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

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

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

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

关于mysql - 如何将查询结果列保存到同一数据库的表中的列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35516215/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com