代码之家  ›  专栏  ›  技术社区  ›  Сашко Мицевски

使用另一个表中的值更新一个表中的值

  •  0
  • Сашко Мицевски  · 技术社区  · 7 年前

    我有 table1 table2 . 它们有相同的列和 ID 就是我可以用来连接表的那个。

    如何运行将更新行的每个语句 Name 在里面 表1 具有列的值 名称 在里面 表2 ? 我需要这个,这样我才能修好柱子 名称 在里面 Table1 因为它是不直接的,它的好值在 表2

    我尝试使用一条update语句,但执行起来需要很长时间,因为两个表都有60多万行

     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
          ); 
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Gordon Linoff    7 年前

    对于此查询:

    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
  •  -1
  •   Vidmantas Blazevicius    7 年前

    一个简单的内部联接应该可以做到这一点。

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