[英]update table data if id exists in another table
如果另一張表中存在一行ID,我想更新mysql表數據。
表1有一列稱為ID。 表2也有相同的列。 如果兩個表中都存在一行,則應對其進行更新。
編輯:我想更新table1中ID在table2中存在的每一行。
請幫忙。 提前致謝。 順便說一句,我正在使用PHP。
您可以使用子查詢來檢查其他表中是否存在該行:
update table2 set xyz_column = 'some value'
where id = ?
and exists (select 1 from table1 where id = ?)
或者簡單地:
update table2 set xyz_column = 'some value'
where id = (select id from table1 where id = ?)
根據編輯,即,如果要更新在table2中存在其ID的table1的所有行,則可以使用update join:
update table1 t1
join table2 t2 on t1.id = t2.id
set t1.xyz_column = 'some value';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.