Mysql脚本循环批量插入数组数据

Mysql脚本循环批量插入数组数据


·首次写博客,不知道说点啥,直接上点干货,希望大家指出不足之处,共同进步。

涉及到Mysql知识点

存储过程、嵌套循环、mysql数组概念、变量、截取字符串、字符串去掉指定字符

脚本研究背景

测试小伙伴需要大量压测数据,而且需要用户ID和mobile对应起来。四处百度以及问了熟悉mysql数据库的朋友,然后整理了下面的脚本,与君分享。

Mysql完整脚本

/*数据表中涉及到某一列必填唯一值,唯一值的处理*/
-- 清除ordersn中唯一值时间的小数点
update ecjia_order_info set order_sn = REPLACE(order_sn,'.','') where order_sn like '%.%';

-- 批量创建数据
drop procedure if exists perorder;
delimiter //
create procedure perorder() 
begin
   declare userstr TEXT;
   declare commanum int;/*数组长度*/
   declare i int default 1;
   declare j int default 0;
   declare uid int default 0;
   declare mobile varchar(16) default '1';

set userstr="600_18702725261,601_18702725262,602_18702725263,603_18702725264,604_18702725265,605_18702725266,606_18702725267,607_18702725268,608_18702725269,609_18702725270,610_18702725271,611_18702725272,612_18702725273,613_18702725274,614_18702725275,615_18702725276,616_18702725277,617_18702725278,618_18702725279";

/*数组长度=逗号个数+1*/
select length(userstr) - length(REPLACE(userstr,',',''))+1 into @commanum;

/*@commanum 赋值的第二种写法*/
-- set @commanum := length(userstr) - length(REPLACE(userstr,',',''));
-- select @commanum;/*执行可查看数组数量*/
	WHILE i <= @commanum DO 
		/*遍历每个用户的uid,mobile*/
		select substring_index(substring_index(userstr,',',i),'_',1),
		substring_index(substring_index(userstr,',',i),'_',-1)
		into @uid,@mobile;
		/*循环插入数据*/
		set j = 0;/*j不可忘记归0*/
		WHILE j < 100 DO 
			INSERT INTO ecjia_order_info 
			(store_id, order_sn, user_id, order_status, shipping_status, pay_status, consignee, country, province, city, district, street, address, longitude, latitude, zipcode, tel, mobile, email, best_time, sign_building, postscript, shipping_id, shipping_name, expect_shipping_time, pay_id, pay_name, how_oos, how_surplus, pack_name, card_name, card_message, inv_payee, inv_content, goods_amount, shipping_fee, insure_fee, pay_fee, pack_fee, card_fee, money_paid, surplus, integral, integral_money, bonus, order_amount, from_ad, referer, add_time, confirm_time, pay_time, shipping_time, auto_delivery_time, pack_id, card_id, bonus_id, invoice_no, extension_code, extension_id, to_buyer, pay_note, agency_id, inv_type, tax, is_separate, parent_id, discount, is_delete, delete_time, is_settlement, sign_time) 
			VALUES ('62',unix_timestamp(current_timestamp(6)),@uid,'0', '0', '0', '压测数据', 'CN', 'CN11', 'CN1101', 'CN110101', ' ', '北京北京市东城区cs', NULL, NULL, '', '',@mobile	, '', '', '', '', '3', '顺丰速运', '', '10', '支付宝', NULL, NULL, NULL, NULL, '', NULL, '', '1062.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0', '0.00', '0.00', '1062.00', '0', 'mobile', '1543521317', '0', '0', '0', '0', '0', '0', '0', NULL, '', '0', NULL, NULL, '0', '', '0.00', '0', '0', '0.00', '0', NULL, '0', '0');			
		set j=j+1;
		end while;
	set i=i+1;
	end while;
end
//执行存储过程
call perorder();

猜你喜欢

转载自blog.csdn.net/weixin_44439463/article/details/86571411
今日推荐