mysql批量复制表数据到另外几张表的写法
生活随笔
收集整理的這篇文章主要介紹了
mysql批量复制表数据到另外几张表的写法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
delimiter //
create procedure oneKey(in newNames varchar(1000),in oldName varchar(250),in id INT,in count INT)
BEGINdeclare num int; -- 定義變量給循環(huán)使用set num = 0;while num < countdoSET @sqlStmt = CONCAT('insert into ', -- 要動態(tài)修改表名需要concat()方法substring_index(substring_index(newNames,',',(0-count+num)),',',1), -- 循環(huán)截取字符串表名' select * from ',oldName,' where id = ',id); -- 源數(shù)據(jù)表名PREPARE stmt FROM @sqlStmt;EXECUTE stmt;set num=num+1;end while;
END;
//
delimiter ;
ps : 特別注意concat()方法中拼接的sql語句,逗號拼接處要留有空格!否則會出錯!!!
newNames : 要復(fù)制數(shù)據(jù)進(jìn)去的表名組成的字符串(例:“'table1','table2','table3','table4',……”)
oldName : 源數(shù)據(jù)表
id : where條件(不需要可以不要)
count : 要復(fù)制的新表個數(shù)
【重點理解批量的概念 : 程序中循環(huán)操作數(shù)據(jù)庫不能算是批量操作!把數(shù)據(jù)一次性給數(shù)據(jù)庫,數(shù)據(jù)庫自己去循環(huán)操作,這種才能算批量操作!!】
?
轉(zhuǎn)載于:https://www.cnblogs.com/xuehuashanghe/p/9531160.html
總結(jié)
以上是生活随笔為你收集整理的mysql批量复制表数据到另外几张表的写法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 添加服务引用的本质是什么?
- 下一篇: 算法题测试用例记录