【问题标题】:insert into table1 from table2 with condition从 table2 插入到 table1 有条件
【发布时间】:2016-04-28 22:53:03
【问题描述】:

我有两个表,我想在 table2 的 table1 的新列 (Orders) 之一中插入数据。请任何帮助,下面的代码不起作用

Insert into Table1 (Orders)
  Select (orders) from table2,
  Where table1.id=table2.id

【问题讨论】:

  • 您不想update Table1 中的现有行吗?
  • 我在表 1 中有新列“订单”。我想使用 table2 中的 vales 填充整个列。
  • 启动时table1是否为空?您想为 table2 中的这些值插入新行到 table1 中吗?还是要修改 table1 中的现有行?
  • Table1 现在有 5 列,其中 4 列已经填满了数据。但是第五列(订单)是新添加的列,默认为 NULL。
  • 感谢 jarlh,从 irakliG 获得了解决方案。谢谢伊拉克利格。

标签: sql sql-server


【解决方案1】:

试试这个:

update t1
set t1.orders=t2.orders
from  Table1 t1
inner join table2 t2
on t1.id=t2.id

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多