[英]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.