牛骨文教育服务平台(让学习变的简单)
博文笔记

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

创建时间:2016-03-23 投稿人: 浏览次数:2032
一、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 ...;
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。