【问题标题】:Update values in one table with values in another table用另一个表中的值更新一个表中的值
【发布时间】:2018-02-07 21:59:51
【问题描述】:

我有table1table2。它们具有相同的列,而 ID 列是我可以用来连接表的列。

我如何运行 foreach 语句,它将用 table2 中的列 Name 的值更新 table1 中的行 Name? 我需要这个,所以我可以修复Table1 中的Name 列,因为它不正确,它的好值在table2

我尝试使用单个更新语句,但它需要很长时间才能执行,因为两个表都有超过 600 000 行

 update 
  table1 t1
set
  (
    t1.name
      ) = (
    select
      t2.name
    from
      table2  t2
    where
      t2.id = t1.id
    and
      rownum = 1    
     )
    where exists (
      select 
        null
      from 
        table2 t2
      where 
        t2.id = t1.id
      ); 

【问题讨论】:

  • 到目前为止你尝试了什么????
  • 我尝试使用一次更新,但这些表的行数超过 600000 行,执行 update table1 t1 set ( t1.name ) = ( select t2.name from table2 t2 where t2.id 需要很长时间= t1.id and rownum = 1 ) where exists (select null from table2 t2 where t2.id = t1.id );
  • 不是评论,而是编辑您的问题。
  • 我认为你应该谷歌一个教程
  • 你试过谷歌吗???这个链接是谷歌出来的第一个结果。 stackoverflow.com/questions/2446764/…

标签: sql oracle join sql-update


【解决方案1】:

对于这个查询:

update table1 t1
    set t1.name = (select t2.name from table2  t2 where t2.id = t1.id and  rownum = 1)
    where exists (select 1
                  from table2 t2
                  where t2.id = t1.id
                 ); 

您希望在table2(id, name) 上建立索引。

【讨论】:

    【解决方案2】:

    一个简单的内部连接应该可以解决这个问题。

    UPDATE T1
    SET T1.NAME = T2.NAME 
    FROM MyTable T1
    INNER JOIN MyOtherTable T2 
    ON T1.ID = T2.ID
    

    【讨论】:

      猜你喜欢
      • 2020-03-18
      • 2016-11-09
      • 2012-08-02
      • 2021-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多