您现在的位置是:网站首页> 编程资料编程资料

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

2023-05-27 408人已围观

简介 mysql批量新增和存储的方法实例_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批量新增存储内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

-六神源码网