【问题标题】:Copy field from one table to another where fields match将字段从一个表复制到字段匹配的另一个表
【发布时间】:2014-02-21 19:48:26
【问题描述】:

我有两个表需要导入数据,这两个表共享一个包含相同数据的列。但是,一个表中有一个值,需要复制到另一个表中。我需要一个如下所示的 sql 命令:

使用 table2 标题更新 table1 contenttitle,其中 table2 id 等于 table1 contentid

所以本质上它是从 table2 title 复制值并将它们输入到 table 1 contenttitle 中,其中 table2 id 和 table1 contentid 中的值相等。

【问题讨论】:

    标签: mysql sql copy


    【解决方案1】:
    update table1 
    join table2 on table1.contentid = table2.id
    set contenttitle = table2.title
    

    【讨论】:

      【解决方案2】:
      Update table1
      set table1.content1=table2.content2
      from table2 join table1 on table2.contentid=table1.contentid
      

      【讨论】:

        【解决方案3】:
        update table1
        set table1.contenttitle = table2.title
        FROM table1 
        inner join table2 on table1.id= table2.id
        

        【讨论】:

        • 虽然这可能会回答问题,但最好添加一些关于此答案如何有助于解决问题的描述。请阅读How do I write a good answer了解更多信息。
        猜你喜欢
        • 1970-01-01
        • 2012-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多