[英]SQL Add to Existing Table from Temp Table
I'm making a basketball stats database, and have a table in SQL with the player's totals from all of their games combined. 我正在建立一个篮球统计数据库,并且在SQL中有一张表格,其中包含所有球员所有比赛的总和。 I'm having trouble adding to it, however.
不过,我在添加时遇到了麻烦。 I use a temporary table for each game, and then I need to add all of the temporary values to the player's total table.
我为每个游戏使用一个临时表,然后将所有临时值添加到玩家的总表中。 How would I do this?
我该怎么做? I have tried many methods, each of which throw an error.
我尝试了很多方法,每个方法都会引发错误。 Here are a few:
这里有一些:
(live_players is the temporary table, and here I'm only trying to join the points column.) (live_players是临时表,在这里我仅尝试加入points列。)
db.execute("UPDATE players JOIN live_players AS t1 ON players.player_id
= t1.player_id SET points = points + t1.points")
db.execute("MERGE INTO players USING live_players ON players.player_id
= live_players.player_id WHEN MATCHED THEN UPDATE
SET points = points + live_players.points")
This should will give you the player_id and points for records in live_players that are new (not sure if that's possible) or have matching records in the players table, along with the addition of that points when player currently exist: 这将为您提供player_id和新的live_players(不确定是否可行)中或在players表中具有匹配记录的记录点,以及在当前存在播放器时添加的点数:
SELECT ST1.player_id, case when ST2.points > 0 THEN ST1.points+ST2.points Else ST1.points END as test FROM live_players ST1 LEFT JOIN players ST2 USING(player_id)
Integrating that with an UPDATE
operation, depending on the unique indexing of our players table should get you where you need to be. 根据我们的玩家表的唯一索引,将其与
UPDATE
操作集成在一起可以使您到达所需的位置。
I think your update statement is wrong. 我认为您的更新声明有误。
You do not have to make a join direct behind the update statement, it has to be put into the FROM clause. 您不必直接在update语句后面进行联接,而必须将其放入FROM子句中。
UPDATE table_to_update
SET what_you_want_to_set
FROM define_the_table(s)
WHERE some_restrictions (maybe with sub select)
Have a look here: 在这里看看:
How can I do an UPDATE statement with JOIN in SQL? 如何在SQL中使用JOIN执行UPDATE语句?
or here: 或在这里:
SQL update query using joins 使用联接的SQL更新查询
Best, 最好,
me 我
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.