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 ...;
因为数据库不能同时更新多条记录,所以需要采用一些间接的方法来实现此项功能
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 ...;
相关文章推荐
- MySQL显示命令
- mysql 命令符操作登陆 展开 查看对象 查看数据表名称 数据表结构 插入
- MySql游标的使用
- mysql事务的提交和回滚和数据引擎有关
- XAMPP打开MySql出现错误
- 简单的mysql 性能和健康程度巡检
- 理解Mysql prepare预处理语句
- 理解MySQL变量和条件
- mysql数据库巡检脚本
- MySQL的Innodb缓存相关优化
- mysql primary partition分区
- mysql中You can’t specify target table for update in FROM clause错误解决方法
- wampserver中mysql数据库的空密码怎么修改
- windows下配置非安装版的MySQL5.6
- 一千行MySQL学习笔记
- mysql之触发器
- [Mysql] find_in_set()使用注意
- mysql访问被拒绝解决 ---谷营中西软件科技园
- Mysql高并发优化
- 5.7 mysqldump 问题