简体   繁体   English

SQL从临时表添加到现有表

[英]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.

相关问题 有条件地从 Python 在 SQL 中创建临时表 - Conditionally Create Temp Table in SQL from Python 将来自云存储的外部临时表与预先存在的大查询表相结合 - append 来自 python - Combining external temp table from cloud storage with pre existing big query table- append from python SQL-使用临时表更新主表 - SQL - update main table using temp table 使用 Snowflake python 连接器从 pandas 添加然后查询临时表 - Add and then query temp table from pandas with Snowflake python connector Python SQL Server - 从现有表中的值创建表 - Python SQL Server - Create Table from Values from an existing Table 在python中使用pyodbc/turbodc从现有表创建SQL表 - create SQL table from existing table using pyodbc/turbodc in python SQL Server临时表在pyodbc代码中不可用 - SQL Server temp table not available in pyodbc code 将 Pandas 数据框插入 SQL 临时表 - Insert pandas data frame into SQL temp table DB2 通过 SQL 魔术“PERSIST”将主键添加到从 Jupyter 实验室创建的现有表中 - DB2 add Primary Key to existing table created from Jupyter Lab via SQL magic 'PERSIST' 将具有来自 python 列表的值的新列插入到现有 sql 表中 - Insert new column with values from a python list into an existing sql table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM