基于 ID 匹配从一个表到另一个表的 SQL 更新
我有一个包含帐号
和卡号
的数据库。 我将它们与一个文件相匹配,以将任何卡号更新为帐号,以便我只使用帐号。
我创建了一个将表链接到帐户/卡数据库的视图,以返回表 ID
和相关帐号,现在我需要更新 ID 与帐号匹配的记录。
这是 Sales_Import
表,其中的 帐号
字段需要更新:
LeadID | AccountNumber |
---|---|
147 | 5807811235 |
150 | 5807811326 |
185 | 7006100100007267039 |
这是 RetrieveAccountNumber
表,我需要从其中更新:
LeadID | AccountNumber |
---|---|
147 | 7006100100007266957 |
150 | 7006100100007267039 |
我尝试了以下操作,但到目前为止没有运气:
UPDATE [Sales_Lead].[dbo].[Sales_Import]
SET [AccountNumber] = (SELECT RetrieveAccountNumber.AccountNumber
FROM RetrieveAccountNumber
WHERE [Sales_Lead].[dbo].[Sales_Import]. LeadID =
RetrieveAccountNumber.LeadID)
它将卡号更新为帐号,但帐号被 NULL
替换
I have a database with account numbers
and card numbers
. I match these to a file to update
any card numbers to the account number so that I am only working with account numbers.
I created a view linking the table to the account/card database to return the Table ID
and the related account number, and now I need to update those records where the ID matches the Account Number.
This is the Sales_Import
table, where the account number
field needs to be updated:
LeadID | AccountNumber |
---|---|
147 | 5807811235 |
150 | 5807811326 |
185 | 7006100100007267039 |
And this is the RetrieveAccountNumber
table, where I need to update from:
LeadID | AccountNumber |
---|---|
147 | 7006100100007266957 |
150 | 7006100100007267039 |
I tried the below, but no luck so far:
UPDATE [Sales_Lead].[dbo].[Sales_Import]
SET [AccountNumber] = (SELECT RetrieveAccountNumber.AccountNumber
FROM RetrieveAccountNumber
WHERE [Sales_Lead].[dbo].[Sales_Import]. LeadID =
RetrieveAccountNumber.LeadID)
It updates the card numbers to account numbers, but the account numbers get replaced by NULL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(29)
对于 SQL Server 2008 + 使用
MERGE
而不是专有的UPDATE ... FROM
语法具有一定的吸引力。除了是标准 SQL 因而更加可移植之外,如果源端存在多个连接行(因此在更新中可能使用多个不同的值),它也会引发错误,而不是让最终结果不确定。
不幸的是,使用哪个的选择可能并不纯粹取决于首选的风格。 SQL Server 中 MERGE 的实现受到各种错误的影响。 Aaron Bertrand 编制了此处报告的列表 。
For SQL Server 2008 + Using
MERGE
rather than the proprietaryUPDATE ... FROM
syntax has some appeal.As well as being standard SQL and thus more portable it also will raise an error in the event of there being multiple joined rows on the source side (and thus multiple possible different values to use in the update) rather than having the final result be undeterministic.
Unfortunately the choice of which to use may not come down purely to preferred style however. The implementation of
MERGE
in SQL Server has been afflicted with various bugs. Aaron Bertrand has compiled a list of the reported ones here.未来开发人员的通用答案。
SQL Server
Oracle(和 SQL Server)
MySQL
Generic answer for future developers.
SQL Server
Oracle (and SQL Server)
MySQL
对于 PostgreSQL:
For PostgreSQL:
看来你使用的是MSSQL,那么,如果我没记错的话,它是这样完成的:
Seems you are using MSSQL, then, if I remember correctly, it is done like this:
对于在
barfoo
行,我也遇到了同样的问题,将foo.new
设置为null
代码>. 我在Oracle中做了这样的事情:I had the same problem with
foo.new
being set tonull
for rows offoo
that had no matching key inbar
. I did something like this in Oracle:以下是在 SQL Server 中对我有用的内容:
Here's what worked for me in SQL Server:
对于运行良好的 MySql:
For MySql that works fine:
感谢您的回复。 我找到了一个解决方案。
Thanks for the responses. I found a solution tho.
如果表位于不同的数据库中。 (MSSQL)
In case the tables are in a different databases. (MSSQL)
使用以下查询块根据 ID 将 Table1 更新为 Table2:
这是解决此问题的最简单方法。
Use the following block of query to update Table1 with Table2 based on ID:
This is the easiest way to tackle this problem.
微软 SQL
甲骨文 11g
MS Sql
Oracle 11g
根据 id 匹配从一张表更新到另一张表
update from one table to another table on id matched
有人建议的下面的 SQL 在 SQL Server 中不起作用。 这种语法让我想起了我以前的学校课程:
不推荐使用
NOT IN
或NOT EXISTS
的所有其他查询。 出现 NULL 是因为 OP 将整个数据集与较小的子集进行比较,那么当然会出现匹配问题。 必须通过使用正确的JOIN
编写正确的 SQL 来解决此问题,而不是使用NOT IN
来避免问题。 在这种情况下,使用NOT IN
或NOT EXISTS
可能会遇到其他问题。我投票给最上面的一个,这是通过加入 SQL Server 来基于另一个表更新一个表的传统方法。 正如我所说,您不能在 SQL Server 中的同一个 UPDATE 语句中使用两个表,除非您先将它们连接起来。
The below SQL someone suggested, does NOT work in SQL Server. This syntax reminds me of my old school class:
All other queries using
NOT IN
orNOT EXISTS
are not recommended. NULLs show up because OP compares entire dataset with smaller subset, then of course there will be matching problem. This must be fixed by writing proper SQL with correctJOIN
instead of dodging problem by usingNOT IN
. You might run into other problems by usingNOT IN
orNOT EXISTS
in this case.My vote for the top one, which is conventional way of updating a table based on another table by joining in SQL Server. Like I said, you cannot use two tables in same
UPDATE
statement in SQL Server unless you join them first.这是 Mysql 和 Maria DB 中最简单、最好的方法。
注意:如果您根据您的 Mysql/Maria DB 版本遇到以下错误“错误代码:1175。您正在使用安全更新模式,并且尝试在没有安全更新的情况下更新表”。 WHERE 使用 KEY 列要禁用安全模式,请在“首选项”中切换该选项,
然后运行如下代码
This is the easiest and best have seen for Mysql and Maria DB
Note: If you encounter the following error based on your Mysql/Maria DB version "Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences"
Then run the code like this
总结其他答案,仅当“匹配存在”时,如何使用另一个表中的数据更新目标表有 4 种变体
查询和子查询:
内连接:
交叉连接:
合并:
所有变体都更不那么琐碎且易于理解,我个人更喜欢“内部连接”选项。 但它们中的任何一个都可以使用,开发人员必须根据他/她的需求选择“更好的选项”
从性能角度来看,带有 join-s 的变体更可取:
Summarizing the other answers, there're 4 variants of how to update target table using data from another table only when "match exists"
Query and sub-query:
Inner join:
Cross join:
Merge:
All variants are more-less trivial and understandable, personally I prefer "inner join" option. But any of them could be used and developer has to select "better option" according to his/her needs
From performance perspective variants with join-s are more preferable:
它与 postgresql 一起使用
it works with postgresql
在同一个表中更新:
update within the same table:
我认为这是一个简单的例子,可能有人会更容易理解,
I thought this is a simple example might someone get it easier,
试试这个:
try this :
MYSQL(这是我基于主键
id
等效性恢复所有特定列reasonId
值的首选方法)MYSQL (This is my preferred way for restoring all specific column
reasonId
values, based on primary keyid
equivalence)这将允许您根据在另一个表中找不到的列值来更新表。
这将根据两个表中找到的列值更新表。
This will allow you to update a table based on the column value not being found in another table.
This will update a table based on the column value being found in both tables.
甲骨文11g
Oracle 11g
对于 Oracle SQL 尝试使用别名
For Oracle SQL try using alias
我想补充一件事。
不要使用相同的值更新值,它会生成额外的日志记录和不必要的开销。
请参见下面的示例 - 尽管链接了 3 条记录,但它只会对 2 条记录执行更新。
I'd like to add one extra thing.
Don't update a value with the same value, it generates extra logging and unnecessary overhead.
See example below - it will only perform the update on 2 records despite linking on 3.
ORACLE
使用
ORACLE
use
从 table2 dpu 更新 table1 dpm set col1 = dpu.col1 其中 dpm.parameter_master_id = dpu.parameter_master_id;
update table1 dpm set col1 = dpu.col1 from table2 dpu where dpm.parameter_master_id = dpu.parameter_master_id;
如果以上答案不适合您,请尝试此操作
If above answers not working for you try this
我相信带有
JOIN
的UPDATE FROM
会有所帮助:MS SQL
MySQL 和 MariaDB
I believe an
UPDATE FROM
with aJOIN
will help:MS SQL
MySQL and MariaDB
将内容从一个表复制到另一个表的简单方法如下:
您还可以添加条件来复制特定数据。
The simple Way to copy the content from one table to other is as follow:
You can also add the condition to get the particular data copied.