【问题标题】:Update table with joining another table通过加入另一个表来更新表
【发布时间】:2012-10-18 12:18:45
【问题描述】:

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

【问题讨论】:

  • 如果您在此处发布答案之前先尝试一下自己,对您的职业生涯会更好。

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


【解决方案1】:

你可以试试这个(对于 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

【讨论】:

  • sql-developer 标签与Oracle相关
  • @Grisha:不,误解,它也可以用于MySql、Access、SQL-Server。
  • @hims056 - for normal SQL 很有趣 :)
【解决方案2】:
update t1
set f1 = 122
where exists (select 1 from t2 where f34 = t1.f34 and f2 = 134)

【讨论】:

    【解决方案3】:
    update t1 a,t2 b set a.f1='122' where a.f34=b.f34 and b.f2=134;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-23
      • 2019-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多