您的位置:首页 > 数据库 > MySQL

MySql同时更新多条记录的方法

2016-03-23 13:46 591 查看
一、MySql同时更新多条记录的方法:

因为数据库不能同时更新多条记录,所以需要采用一些间接的方法来实现此项功能

1、可以新建一张临时表,让后将查找需要更新的数据插入临时表中,然后将临时表中的数据同步到需要更新的表中,

创建临时表:create
temporary table temp_table (name varchar(128) not null, age int not null);

向临时表中插入数据:create
temporary table temp_table select * from table_name where
...

同步两个数据表中相同字段的数据:

insert
into table_name(f_user_id) select t.f_user_id from temp_table as temp left join table_name

as table on (table.f_user_id = temp.f_user_id and ...) where ...;

2、不建立临时表直接更新:

update table_name as table, temp_table as temp

set table.f_update_column = temp.f_update_column

where table.f_user_id = temp.f_user_id and ...;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: