mysql批量新增和存储的方法实例

登录压测时,需要很多不同的用户,此时需要向数据库新增数据

#批量添加用户账号——存储过程:
delimiter //
drop procedure if exists test;
create procedure test()
 
begin
declare i int;
set i = 1;
while i<21 do
insert into hg_user values (concat("om_test",cast(i as char)),concat("om_test",cast(i as char)),"f1b2f5b9fbc8b513",null,null,null,null,null,null,null,null,null,null,null,null,null,null,null);
set i = i+1;
end while;
select * from test;
end//
call test();

delimiter是mysql分隔符,在mysql客户端中分隔符默认是分号(;)。

如果一次输入的语句较多,并且语句中间有分号,这时需要新指定一个特殊的分隔符,常用//,&&。

上面就是,先将分隔符设置为 //,

直到遇到下一个 //,才整体执行语句。

执行完后,最后一行, delimiter ; 将mysql的分隔符重新设置为分号;

如果不修改的话,本次会话中的所有分隔符都以// 为准。

concat 是字符连接,将多个字符串连接成一个字符串.

语法:concat(str1, str2,…)

eg:select concat (id, name, score) as info from tt2;     1小明60

cast函数用于将某种数据类型的表达式显式转换为另一种数据类型。

语法:cast (expression as data_type)

可以转换的类型是有限制的。这个类型可以是以下值其中的一个:

  • 二进制,同带binary前缀的效果 : binary   
  • 字符型,可带参数 : char()    
  • 日期 : date    
  • 时间: time    
  • 日期时间型 : datetime    
  • 浮点数 : decimal     
  • 整数 : signed    
  • 无符号整数 : unsigned 

批量删除方案(删除用户也一样)

#删除解决方案——存储过程;
delimiter //
drop procedure if exists test;
create procedure test()
 
begin
declare i int;
set i = 1;
while i<11 do
delete from hg_application_flow_template where user_name=concat("om_test",cast(i as char));
delete from hg_application_flow_template_details where created_by=concat("om_test",cast(i as char));
set i = i+1;
end while;
select * from test;
end//
call test();

总结

到此这篇关于mysql批量新增和存储的文章就介绍到这了,更多相关mysql批量新增存储内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐