【问题标题】:Overwrite values from one table to another using使用将值从一个表覆盖到另一个表
【发布时间】:2021-02-08 07:25:57
【问题描述】:

我有两张表,customer 和 newCustomer。对于特定列,我想用 newCustomer 表覆盖客户表的列值。例如:

customer.firstname = newCustomer.firstname
customer.lastname = newCustomer.lastname

** 我确实有两个表的匹配 ID。

我可以在编码方面考虑如何做到这一点,但在考虑用 SQL 做到这一点时遇到了困难。

我正在使用 Microsoft SQL Server。

希望有任何示例或提示。

【问题讨论】:

  • 请提供样本数据和期望的结果。
  • @GordonLinoff 我有一个名为 Customer 和 newCustomer 的表。所以我想让 customer.firstname = newCustomer.lastname.

标签: sql sql-server datatable overwrite


【解决方案1】:

大概,你有一些 id 连接这两个表。如果是这样,只需使用joinset

update c
    set firstname = nc.firstname,
        lastname = nc.lastname
    from customer c join
         newcustomer nc
         on c.customerid = nc.customerid;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多