赞
踩
-- 注意mysql不支持该语法
select * into target_table from source_table;
-- mysql中可使用下面代替select ... into
Create table Table2 (Select * from f6dos.1);
Create table Table3 (Select q from f6dos.1);
-- 常用这个语句
insert into target_table(column1,column2) select column1,5 from source_table;
以上两句都是将源表source_table的记录插入到目标表target_table,但两句又有区别。
第一句(select into from)要求目标表target_table不存在,因为在插入时会自动创建。mysql暂时不支持该语法
第二句(insert into select from)要求目标表target_table存在,由于目标表已经存在,所以我们除了插入源表source_table的字段外,还可以插入常量,
当迁移的表数据量比较多大的时候危险。
insert into … select容易造成死锁的原因,后面的select语句对后表会逐步加s锁,前面的insert数量不一定,导致锁住另一个表整表auto-inc锁。 锁越多越容易出现死锁问题。
-- 复制表结构,新建临时表
create table gb_user_sel_history_tmp like gb_user_sel_history ;
-- 复制数据
insert into table1
select * from table2 where create_date >'2019-09-15';
-- 快速切换表
rename table table1 to table2;
注意: select 后面的表语句可以是复合语句,包括各种连表操作。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。