更新表并加入另一个表

时间:2012-10-29 07:11:47

标签: sql oracle join sql-update oracle-sqldeveloper

如果表f1中的字段t1的值为122,我想在表f2中设置字段t2,其值为134。假设字段f34是表t1t2之间的关键字 我将如何在SQL Developer中为此编写查询?

3 个答案:

答案 0 :(得分:3)

你可以尝试这个(对于Oracle):

UPDATE ( SELECT t1.f1, t2.f2
           FROM t1 
           JOIN t2
             ON t1.f34 = t2.f34
          WHERE t2.f2 = 134
       )
SET f1 = 122;

See this SQLFiddle

对于其他RDBMS,请尝试使用join

进行更新

对于SQL Server:

UPDATE t1 
SET    f1 = 122
FROM   t1 
       JOIN t2  
         ON t1.f34 = t2.f34
WHERE  t2.f2 = 134

See this SQLFiddle

对于MySQL

UPDATE t1 temp1
  JOIN t2 temp2
    ON temp1.f34 = temp2.f34
SET    temp1.F1 = 122
WHERE  temp2.f2 = 134;

See this SQLFiddle

答案 1 :(得分:1)

update t1
set f1 = 122
where exists (select 1 from t2 where f34 = t1.f34 and f2 = 134)

答案 2 :(得分:0)

update t1 a,t2 b set a.f1='122' where a.f34=b.f34 and b.f2=134;