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

吾爱主题 阅读:131 2024-04-02 08:01:22 评论:0

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

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #批量添加用户账号——存储过程: 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 

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

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #删除解决方案——存储过程; 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批量新增存储内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/hjy123/p/14626820.html

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容