SQL update from one Table to another based on a ID match

后端 未结 22 1289
太阳男子
太阳男子 2020-11-21 22:49

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

相关标签:
22条回答
  • 2020-11-21 23:21

    If above answers not working for you try this

    Update Sales_Import A left join RetrieveAccountNumber B on A.LeadID = B.LeadID
    Set A.AccountNumber = B.AccountNumber
    where A.LeadID = B.LeadID 
    
    0 讨论(0)
  • 2020-11-21 23:22

    Seems you are using MSSQL, then, if I remember correctly, it is done like this:

    UPDATE [Sales_Lead].[dbo].[Sales_Import] SET [AccountNumber] = 
    RetrieveAccountNumber.AccountNumber 
    FROM RetrieveAccountNumber 
    WHERE [Sales_Lead].[dbo].[Sales_Import].LeadID = RetrieveAccountNumber.LeadID
    
    0 讨论(0)
  • 2020-11-21 23:24

    Generic answer for future developers.

    SQL Server

    UPDATE 
         t1
    SET 
         t1.column = t2.column
    FROM 
         Table1 t1 
         INNER JOIN Table2 t2 
         ON t1.id = t2.id;
    

    Oracle (and 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;
    
    0 讨论(0)
  • 2020-11-21 23:24

    MS 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';
    

    Oracle 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  ;
    
    0 讨论(0)
  • 2020-11-21 23:24

    update within the same table:

      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
    
    0 讨论(0)
  • 2020-11-21 23:27

    For PostgreSQL:

    UPDATE Sales_Import SI
    SET AccountNumber = RAN.AccountNumber
    FROM RetrieveAccountNumber RAN
    WHERE RAN.LeadID = SI.LeadID; 
    
    0 讨论(0)
提交回复
热议问题