I want to set field f1
in table t1
with the value 122
if field f2
in table t2
has value 134
. Suppose field f34
is the key between table t1
and t2
.
How would I write the query for this in SQL Developer?
问问题
1351 次
3 回答
3
你可以试试这个(对于 Oracle):
UPDATE ( SELECT t1.f1, t2.f2
FROM t1
JOIN t2
ON t1.f34 = t2.f34
WHERE t2.f2 = 134
)
SET f1 = 122;
看到这个 SQLFiddle
对于其他 RDBMS,请尝试使用 join 进行更新
对于 SQL Server:
UPDATE t1
SET f1 = 122
FROM t1
JOIN t2
ON t1.f34 = t2.f34
WHERE t2.f2 = 134
看到这个 SQLFiddle
对于 MySQL
UPDATE t1 temp1
JOIN t2 temp2
ON temp1.f34 = temp2.f34
SET temp1.F1 = 122
WHERE temp2.f2 = 134;
看到这个 SQLFiddle
于 2012-10-29T07:14:59.017 回答
1
update t1
set f1 = 122
where exists (select 1 from t2 where f34 = t1.f34 and f2 = 134)
于 2012-10-29T07:16:35.473 回答
0
update t1 a,t2 b set a.f1='122' where a.f34=b.f34 and b.f2=134;
于 2012-10-29T07:16:38.137 回答