1089

我有一个带有account numbersand的数据库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

4

27 回答 27

1562

我相信UPDATE FROMwith aJOIN会有所帮助:

微软 SQL

UPDATE
    Sales_Import
SET
    Sales_Import.AccountNumber = RAN.AccountNumber
FROM
    Sales_Import SI
INNER JOIN
    RetrieveAccountNumber RAN
ON 
    SI.LeadID = RAN.LeadID;

MySQL 和 MariaDB

UPDATE
    Sales_Import SI,
    RetrieveAccountNumber RAN
SET
    SI.AccountNumber = RAN.AccountNumber
WHERE
    SI.LeadID = RAN.LeadID;
于 2008-10-22T07:19:54.610 回答
320

将内容从一个表复制到另一个表的简单方法如下:

UPDATE table2 
SET table2.col1 = table1.col1, 
table2.col2 = table1.col2,
...
FROM table1, table2 
WHERE table1.memberid = table2.memberid

您还可以添加条件以获取复制的特定数据。

于 2010-01-20T12:26:50.277 回答
162

对于 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;

不幸的是,选择使用哪一个可能不会完全归结为首选风格。SQL Server 中的实现MERGE受到各种错误的困扰。Aaron Bertrand 在这里编制了一份报告的清单。

于 2012-02-11T15:13:15.727 回答
132

未来开发人员的通用答案。

SQL 服务器

UPDATE 
     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;
于 2016-08-29T17:59:14.940 回答
55

对于 PostgreSQL:

UPDATE Sales_Import SI
SET AccountNumber = RAN.AccountNumber
FROM RetrieveAccountNumber RAN
WHERE RAN.LeadID = SI.LeadID; 
于 2014-07-16T19:55:13.500 回答
39

似乎您正在使用MSSQL,那么,如果我没记错的话,它是这样完成的:

UPDATE [Sales_Lead].[dbo].[Sales_Import] SET [AccountNumber] = 
RetrieveAccountNumber.AccountNumber 
FROM RetrieveAccountNumber 
WHERE [Sales_Lead].[dbo].[Sales_Import].LeadID = RetrieveAccountNumber.LeadID
于 2008-10-22T07:21:53.467 回答
36

对于没有匹配键的行foo.new设置为. 我在 Oracle 中做了这样的事情:nullfoobar

更新 foo
设置 foo.new = (选择 bar.new
                  从酒吧
                  其中 foo.key = bar.key)
在哪里存在(选择 1
              从酒吧
              其中 foo.key = bar.key)
于 2009-04-29T18:32:22.233 回答
34

以下是在 SQL Server 中对我有用的方法:

UPDATE [AspNetUsers] SET

[AspNetUsers].[OrganizationId] = [UserProfile].[OrganizationId],
[AspNetUsers].[Name] = [UserProfile].[Name]

FROM [AspNetUsers], [UserProfile]
WHERE [AspNetUsers].[Id] = [UserProfile].[Id];
于 2018-09-07T12:49:12.937 回答
29

对于运行良好的 MySql:

UPDATE
    Sales_Import SI,RetrieveAccountNumber RAN
SET
    SI.AccountNumber = RAN.AccountNumber
WHERE
    SI.LeadID = RAN.LeadID
于 2012-02-13T16:40:03.477 回答
22

感谢您的回复。我找到了解决方案。

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)  
于 2008-10-22T08:07:41.083 回答
20

如果表在不同的数据库中。(MSSQL)

update database1..Ciudad
set CiudadDistrito=c2.CiudadDistrito

FROM database1..Ciudad c1
 inner join 
  database2..Ciudad c2 on c2.CiudadID=c1.CiudadID
于 2019-04-24T04:14:53.453 回答
15

使用以下查询块根据 ID 将 Table1 更新为 Table2:

UPDATE Sales_Import, RetrieveAccountNumber 
SET Sales_Import.AccountNumber = RetrieveAccountNumber.AccountNumber 
where Sales_Import.LeadID = RetrieveAccountNumber.LeadID;

这是解决此问题的最简单方法。

于 2017-11-12T16:34:09.543 回答
12

微软 SQL

UPDATE  c4 SET Price=cp.Price*p.FactorRate FROM TableNamea_A c4
inner join TableNamea_B p on c4.Calcid=p.calcid 
inner join TableNamea_A cp on c4.Calcid=cp.calcid 
WHERE c4..Name='MyName';

甲骨文 11g

        MERGE INTO  TableNamea_A u 
        using
        (
                SELECT c4.TableName_A_ID,(cp.Price*p.FactorRate) as CalcTot 
                FROM TableNamea_A c4
                inner join TableNamea_B p on c4.Calcid=p.calcid 
                inner join TableNamea_A cp on c4.Calcid=cp.calcid 
                WHERE p.Name='MyName' 
        )  rt
        on (u.TableNamea_A_ID=rt.TableNamea_B_ID)
        WHEN MATCHED THEN
        Update set Price=CalcTot  ;
于 2019-04-23T06:30:11.100 回答
8

在 id 匹配时从一个表更新到另一个表

UPDATE 
     TABLE1 t1, 
     TABLE2 t2
SET 
     t1.column_name = t2.column_name 
WHERE
     t1.id = t2.id;
于 2020-12-21T13:25:30.673 回答
7

有人建议的以下 SQL 在 SQL Server 中不起作用。这种语法让我想起了我的老学校课程:

UPDATE table2 
SET table2.col1 = table1.col1, 
table2.col2 = table1.col2,
...
FROM table1, table2 
WHERE table1.memberid = table2.memberid

不推荐使用NOT INor的所有其他查询。NOT EXISTS出现 NULL 是因为 OP 将整个数据集与较小的子集进行比较,那么当然会有匹配问题。这必须通过编写正确的 SQL 来解决,JOIN而不是使用NOT IN. 在这种情况下,使用NOT INor可能会遇到其他问题。NOT EXISTS

我投票给了第一名,这是通过加入 SQL Server 来更新基于另一个表的表的传统方式。就像我说的,你不能UPDATE在 SQL Server 的同一个语句中使用两个表,除非你先加入它们。

于 2016-06-12T20:54:06.687 回答
4

在同一个表中更新:

  DECLARE @TB1 TABLE
    (
        No Int
        ,Name NVarchar(50)
        ,linkNo int
    )

    DECLARE @TB2 TABLE
    (
        No Int
        ,Name NVarchar(50)
        ,linkNo int
    )

    INSERT INTO @TB1 VALUES(1,'changed person data',  0);
    INSERT INTO @TB1 VALUES(2,'old linked data of person', 1);

INSERT INTO @TB2 SELECT * FROM @TB1 WHERE linkNo = 0


SELECT * FROM @TB1
SELECT * FROM @TB2


    UPDATE @TB1 
        SET Name = T2.Name
    FROM        @TB1 T1
    INNER JOIN  @TB2 T2 ON T2.No = T1.linkNo

    SELECT * FROM @TB1
于 2012-12-06T12:24:29.870 回答
4

它适用于 postgresql

UPDATE application
SET omts_received_date = (
    SELECT
        date_created
    FROM
        application_history
    WHERE
        application.id = application_history.application_id
    AND application_history.application_status_id = 8
);
于 2015-05-14T09:30:28.590 回答
4

这是 Mysql 和 Maria DB 所见过的最简单和最好的

UPDATE table2, table1 SET table2.by_department = table1.department WHERE table1.id = table2.by_id

注意:如果您遇到以下基于您的 Mysql/Maria DB 版本的错误“错误代码:1175。您正在使用安全更新模式,并且您尝试更新没有 WHERE 使用 KEY 列的表要禁用安全模式,请切换首选项中的选项”

然后像这样运行代码

SET SQL_SAFE_UPDATES=0;
UPDATE table2, table1 SET table2.by_department = table1.department WHERE table1.id = table2.by_id
于 2021-03-08T11:55:03.290 回答
2

我认为这是一个简单的例子可能有人会更容易,

        DECLARE @TB1 TABLE
        (
            No Int
            ,Name NVarchar(50)
        )

        DECLARE @TB2 TABLE
        (
            No Int
            ,Name NVarchar(50)
        )

        INSERT INTO @TB1 VALUES(1,'asdf');
        INSERT INTO @TB1 VALUES(2,'awerq');


        INSERT INTO @TB2 VALUES(1,';oiup');
        INSERT INTO @TB2 VALUES(2,'lkjhj');

        SELECT * FROM @TB1

        UPDATE @TB1 SET Name =S.Name
        FROM @TB1 T
        INNER JOIN @TB2 S
                ON S.No = T.No

        SELECT * FROM @TB1
于 2012-06-04T11:55:41.443 回答
2

MYSQL (这是我基于主键等效性恢复所有特定列值的首选方式)reasonIdid

UPDATE `site` AS destination  
INNER JOIN `site_copy` AS backupOnTuesday 
      ON backupOnTuesday.`id` = destination.`id`
SET destdestination.`reasonId` = backupOnTuesday.`reasonId`
于 2021-01-06T09:46:14.027 回答
1

尝试这个 :

UPDATE
    Table_A
SET
    Table_A.AccountNumber = Table_B.AccountNumber ,
FROM
    dbo.Sales_Import AS Table_A
    INNER JOIN dbo.RetrieveAccountNumber AS Table_B
        ON Table_A.LeadID = Table_B.LeadID 
WHERE
    Table_A.LeadID = Table_B.LeadID
于 2016-11-14T12:58:49.730 回答
1

对于 Oracle SQL,请尝试使用别名

UPDATE Sales_Lead.dbo.Sales_Import SI 
SET SI.AccountNumber = (SELECT RAN.AccountNumber FROM RetrieveAccountNumber RAN WHERE RAN.LeadID = SI.LeadID);
于 2022-02-22T08:08:03.513 回答
1

甲骨文 11g

merge into Sales_Import
using RetrieveAccountNumber
on (Sales_Import.LeadId = RetrieveAccountNumber.LeadId)
when matched then update set Sales_Import.AccountNumber = RetrieveAccountNumber.AccountNumber;
于 2019-01-21T16:40:30.743 回答
0

这将允许您根据在另一个表中未找到的列值来更新表。

    UPDATE table1 SET table1.column = 'some_new_val' WHERE table1.id IN (
            SELECT * 
            FROM (
                    SELECT table1.id
                    FROM  table1 
                    LEFT JOIN table2 ON ( table2.column = table1.column ) 
                    WHERE table1.column = 'some_expected_val'
                    AND table12.column IS NULL
            ) AS Xalias
    )

这将根据在两个表中找到的列值更新表。

    UPDATE table1 SET table1.column = 'some_new_val' WHERE table1.id IN (
            SELECT * 
            FROM (
                    SELECT table1.id
                    FROM  table1 
                    JOIN table2 ON ( table2.column = table1.column ) 
                    WHERE table1.column = 'some_expected_val'
            ) AS Xalias
    )
于 2015-04-02T15:27:50.667 回答
0

我想补充一件额外的事情。

不要使用相同的值更新值,它会产生额外的日志记录和不必要的开销。请参见下面的示例 - 尽管链接到 3,但它只会对 2 条记录执行更新。

DROP TABLE #TMP1
DROP TABLE #TMP2
CREATE TABLE #TMP1(LeadID Int,AccountNumber NVarchar(50))
CREATE TABLE #TMP2(LeadID Int,AccountNumber NVarchar(50))

INSERT INTO #TMP1 VALUES
(147,'5807811235')
,(150,'5807811326')
,(185,'7006100100007267039');

INSERT INTO #TMP2 VALUES
(147,'7006100100007266957')
,(150,'7006100100007267039')
,(185,'7006100100007267039');

UPDATE A
SET A.AccountNumber = B.AccountNumber
FROM
    #TMP1 A 
        INNER JOIN #TMP2 B
        ON
        A.LeadID = B.LeadID
WHERE
    A.AccountNumber <> B.AccountNumber  --DON'T OVERWRITE A VALUE WITH THE SAME VALUE

SELECT * FROM #TMP1
于 2016-12-13T23:46:41.007 回答
0

甲骨文

利用

UPDATE suppliers
SET supplier_name = (SELECT customers.customer_name
                     FROM customers
                     WHERE customers.customer_id = suppliers.supplier_id)
WHERE EXISTS (SELECT customers.customer_name
              FROM customers
              WHERE customers.customer_id = suppliers.supplier_id);

于 2022-01-28T15:14:07.920 回答
-3

如果上述答案对您不起作用,请尝试此

Update Sales_Import A left join RetrieveAccountNumber B on A.LeadID = B.LeadID
Set A.AccountNumber = B.AccountNumber
where A.LeadID = B.LeadID 
于 2018-01-10T04:10:35.670 回答