代码改变世界

MYSQL用一个表中的字段批量更新另一个表中的字段

2020-02-07 19:13  盛世游侠  阅读(2437)  评论(0编辑  收藏  举报
select ta.id,ta.parent_id,ta.name,ta.level, mr.id as r_id,mr.parent_id as r_pid,mr.name as r_name,mr.type as r_type,mr.provice_id as r_provice_id from test_movhome.area ta, movhome.region mr where ta.name = mr.name


# 批量替换,用一个表中的字段 更新 另一个表中的字段
update test_movhome.cinema a inner join 
(select ta.id,ta.parent_id,ta.name,ta.level, mr.id as r_id,mr.parent_id as r_pid,mr.name as r_name,mr.type as r_type,mr.provice_id as r_provice_id from test_movhome.area ta, movhome.region mr where ta.name = mr.name) b 
on a.region_id = b.r_id set a.region_id = b.id


# 批量替换,用一个表中的字段 更新 另一个表中的字段
update test_movhome.cinema a, 
(select ta.id,ta.parent_id,ta.name,ta.level, mr.id as r_id,mr.parent_id as r_pid,mr.name as r_name,mr.type as r_type,mr.provice_id as r_provice_id from test_movhome.area ta, movhome.region mr where ta.name = mr.name) b 
set a.new_region_id = b.id, a.other_filed = b.other_filed
where a.region_id = b.r_id

# 批量设置一个表中的字段为一个固定的值
update test_movhome.cinema a set a.new_region_id = 0