【问题标题】:update one table with data from another用另一个表的数据更新一个表
【发布时间】:2011-06-29 13:04:50
【问题描述】:

表 1:

id    name    desc
-----------------------
1     a       abc
2     b       def
3     c       adf

表 2:

id    name    desc
-----------------------
1     x       123
2     y       345

如何运行 sql update 查询,该查询可以使用表 2 的名称和 desc 使用相同的 id 更新表 1?所以我得到的最终结果是

表 1:

id    name    desc
-----------------------
1     x       123
2     y       345
3     c       adf

如何做到这一点:

  • SQL 服务器
  • MySQL
  • PostgreSQL
  • 甲骨文

【问题讨论】:

    标签: sql database


    【解决方案1】:

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

    UPDATE Table1, Table2 
    SET Table1.DataColumn= Table2.DataColumn
    where Table1.ID= Table2.ID;
    

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

    【讨论】:

      【解决方案2】:
      UPDATE table1
      SET 
      `ID` = (SELECT table2.id FROM table2 WHERE table1.`name`=table2.`name`)
      

      【讨论】:

        【解决方案3】:

        试试下面的代码。它对我有用....

        UPDATE TableOne 
        SET 
        field1 =(SELECT TableTwo.field1 FROM TableTwo WHERE TableOne.id=TableTwo.id),
        field2 =(SELECT TableTwo.field2 FROM TableTwo WHERE TableOne.id=TableTwo.id)
        WHERE TableOne.id = (SELECT  TableTwo.id 
                                     FROM   TableTwo 
                                     WHERE  TableOne.id = TableTwo.id) 
        

        【讨论】:

        • 这个查询处理速度很慢。
        【解决方案4】:

        Oracle 11g R2:

        create table table1 (
          id number,
          name varchar2(10),
          desc_ varchar2(10)
        );
        
        create table table2 (
          id number,
          name varchar2(10),
          desc_ varchar2(10)
        );
        
        insert into table1 values(1, 'a', 'abc');
        insert into table1 values(2, 'b', 'def');
        insert into table1 values(3, 'c', 'ghi');
        
        insert into table2 values(1, 'x', '123');
        insert into table2 values(2, 'y', '456');
        
        merge into table1 t1
        using (select * from table2) t2
        on (t1.id = t2.id)
        when matched then update set t1.name = t2.name, t1.desc_ = t2.desc_;
        
        select * from table1;
        
                ID NAME       DESC_
        ---------- ---------- ----------
                 1 x          123
                 2 y          456
                 3 c          ghi
        

        另见Oracle - Update statement with inner join

        【讨论】:

        • "匹配后更新集"。看起来你也可以告诉 oracle“在你吃薯条的时候也点一些薯条”XD
        【解决方案5】:

        对于 MySql:

        UPDATE table1 JOIN table2 
            ON table1.id = table2.id
        SET table1.name = table2.name,
            table1.`desc` = table2.`desc`
        

        对于 Sql 服务器:

        UPDATE   table1
        SET table1.name = table2.name,
            table1.[desc] = table2.[desc]
        FROM table1 JOIN table2 
           ON table1.id = table2.id
        

        【讨论】:

        • +1。在 SQL Server 中,我通常使用与您展示的相同的结构化更新。但在这种特殊情况下,FROM 部分可能更简单一些:只需 FROM table2 WHERE table1.id = table2.id,并且可以删除每个分配左侧的别名。
        • @andriy-m 我更喜欢将连接谓词放在 JOIN 而不是 WHERE...
        • 你能更新一下 Oracle 吗?也许确认这两条语句中的任何一条都适用于 Oracle?
        • 我想补充一点:对于 Postgresql,语法略有不同update a set field1 = concat_ws(' ',field1, table2.middle_name, table2.first_name) from table2 where a.id = table2.fieldX; 只是添加了一些功能来展示什么是可能的。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-12-27
        • 1970-01-01
        • 1970-01-01
        • 2012-04-07
        • 2014-02-18
        • 2011-10-25
        相关资源
        最近更新 更多