在使用Mysql开发应用时,难免会有根据一张表(tableA)中的某字段来把数据更新到另一张表(tableB)的操作
需要更新的表: myw_table_a a
查询数据的表: myw_table_b b
方式一(最常用)
update
myw_table_a a, myw_table_b b
set
a.name = b.nick_name, a.phone = b.nick_phone
where
a.id = b.id
方式二(不建议用,仅仅只是更新一个字段数据都有点慢,b表对a表还是一对多关系)
update
myw_table_a a
set
a.name = (select nick_name from myw_table_b b where a.od = b.id)
// 多字段
update
myw_table_a a
set
a.name = (select nick_name from myw_table_b b where a.od = b.id),
a.phone = (select nick_phone from myw_table_b b where a.id = b.id)
方式三(能用,数据量大了就比较慢,比方式二要逊色一些)
update
myw_table_a a join myw_table_b b on a.id=b.id
set
a.name = b.nick_name,
a.phone = b.nick_phone