我有一个带account numbers
和的数据库card numbers
.我将这些文件与update
任何卡号匹配到帐号,以便我只使用帐号.
我创建了一个视图链接表和帐户/卡数据库以返回Table ID
相关的帐号,现在我需要更新ID与帐号匹配的记录.
这是Sales_Import
表,account number
需要更新字段:
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
我相信一个UPDATE FROM
有JOIN
帮助的人:
UPDATE Sales_Import SET Sales_Import.AccountNumber = RAN.AccountNumber FROM Sales_Import SI INNER JOIN RetrieveAccountNumber RAN ON SI.LeadID = RAN.LeadID;
UPDATE Sales_Import SI, RetrieveAccountNumber RAN SET SI.AccountNumber = RAN.AccountNumber WHERE SI.LeadID = RAN.LeadID;
将内容从一个表复制到另一个表的简单方法如下:
UPDATE table2 SET table2.col1 = table1.col1, table2.col2 = table1.col2, ... FROM table1, table2 WHERE table1.memberid = table2.memberid
您还可以添加条件以复制特定数据.
对于SQL Server 2008 +使用MERGE
而不是专有UPDATE ... FROM
语法有一些吸引力.
除了作为标准SQL并因此更具可移植性之外,如果源端存在多个连接行(因此在更新中使用多个可能的不同值)而不是使最终结果不确定,它也会引发错误. .
MERGE INTO Sales_Import USING RetrieveAccountNumber ON Sales_Import.LeadID = RetrieveAccountNumber.LeadID WHEN MATCHED THEN UPDATE SET AccountNumber = RetrieveAccountNumber.AccountNumber;
不幸的是,选择哪种使用可能不会纯粹归结为优选的风格.执行MERGE
SQL Server中已患各种错误.Aaron Bertrand在这里编制了一份报告清单.
未来开发人员的通用答案.
SQL ServerUPDATE t1 SET t1.column = t2.column FROM Table1 t1 INNER JOIN Table2 t2 ON t1.id = t2.id;Oracle(和SQL Server)
UPDATE t1 SET t1.colmun = t2.column FROM Table1 t1, Table2 t2 WHERE t1.ID = t2.ID;MySQL的
UPDATE Table1 t1, Table2 t2 SET t1.column = t2.column WHERE t1.ID = t2.ID;
看来你正在使用MSSQL,如果我没记错的话,它是这样完成的:
UPDATE [Sales_Lead].[dbo].[Sales_Import] SET [AccountNumber] = RetrieveAccountNumber.AccountNumber FROM RetrieveAccountNumber WHERE [Sales_Lead].[dbo].[Sales_Import].LeadID = RetrieveAccountNumber.LeadID
foo.new
设置null
为foo
没有匹配键的行时,我遇到了同样的问题bar
.我在Oracle中做了类似的事情:
update foo set foo.new = (select bar.new from bar where foo.key = bar.key) where exists (select 1 from bar where foo.key = bar.key)
对于PostgreSQL:
UPDATE Sales_Import SI SET AccountNumber = RAN.AccountNumber FROM RetrieveAccountNumber RAN WHERE RAN.LeadID = SI.LeadID;
对于工作正常的MySql:
UPDATE Sales_Import SI,RetrieveAccountNumber RAN SET SI.AccountNumber = RAN.AccountNumber WHERE SI.LeadID = RAN.LeadID
这是在SQL Server中对我有用的:
UPDATE [AspNetUsers] SET [AspNetUsers].[OrganizationId] = [UserProfile].[OrganizationId], [AspNetUsers].[Name] = [UserProfile].[Name] FROM [AspNetUsers], [UserProfile] WHERE [AspNetUsers].[Id] = [UserProfile].[Id];
谢谢你的回复.我发现了一个解决方案.
UPDATE Sales_Import SET AccountNumber = (SELECT RetrieveAccountNumber.AccountNumber FROM RetrieveAccountNumber WHERE Sales_Import.leadid =RetrieveAccountNumber.LeadID) WHERE Sales_Import.leadid = (SELECT RetrieveAccountNumber.LeadID FROM RetrieveAccountNumber WHERE Sales_Import.leadid = RetrieveAccountNumber.LeadID)
使用以下查询块根据ID使用Table2更新Table1:
UPDATE Sales_Import, RetrieveAccountNumber SET Sales_Import.AccountNumber = RetrieveAccountNumber.AccountNumber where Sales_Import.LeadID = RetrieveAccountNumber.LeadID;
这是解决此问题的最简单方法。
如果表位于不同的数据库中。(SQLserver)
update database1..Ciudad set CiudadDistrito=c2.CiudadDistrito FROM database1..Ciudad c1 inner join database2..Ciudad c2 on c2.CiudadID=c1.CiudadID