[英]How can I update some rows using an inner join in SQL in a way which will work on both mysql and postgres?
[英]Update with join that will work on both MySQL and SQL Server
我需要此更新/加入查詢才能在 MySQL 和 SQL 服務器上工作。 我似乎無法弄清楚。 我不確定這是否可能。 這是它的樣子
update table1 a
inner join table2 b on a.id = b.id
set a.name = b.name
誰能幫我改變它,使其適用於 MySQL 和 SQL Sserver? 謝謝!
使用相關子查詢而不是join
:
update table1 a
set a.name = (select b.name from table2 b where a.id = b.id)
where exists (select 1 from table2 b where a.id = b.id);
如果您知道兩個表中的 id 匹配,則不需要外部where
子句。
在 Oracle 中,您可以編寫以下查詢,我不確定它是否也適用於 MySQL 和 SQL 服務器:
update (select a.name n1, b.name n2
from table1 a join table2 b on a.id = b.id)
set n1 = n2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.