mysql批量新增和存儲的方法實例
登錄壓測時,需要很多不同的用戶,此時需要向數(shù)據(jù)庫新增數(shù)據(jù)
#批量添加用戶賬號——存儲過程: 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客戶端中分隔符默認是分號(;)。
如果一次輸入的語句較多,并且語句中間有分號,這時需要新指定一個特殊的分隔符,常用//,&&。
上面就是,先將分隔符設(shè)置為 //,
直到遇到下一個 //,才整體執(zhí)行語句。
執(zhí)行完后,最后一行, delimiter ; 將mysql的分隔符重新設(shè)置為分號;
如果不修改的話,本次會話中的所有分隔符都以// 為準。
concat 是字符連接,將多個字符串連接成一個字符串.
語法:concat(str1, str2,...)
eg:select concat (id, name, score) as info from tt2; 1小明60
cast函數(shù)用于將某種數(shù)據(jù)類型的表達式顯式轉(zhuǎn)換為另一種數(shù)據(jù)類型。
語法:CAST (expression AS data_type)
可以轉(zhuǎn)換的類型是有限制的。這個類型可以是以下值其中的一個:
- 二進制,同帶binary前綴的效果 : BINARY
- 字符型,可帶參數(shù) : CHAR()
- 日期 : DATE
- 時間: TIME
- 日期時間型 : DATETIME
- 浮點數(shù) : DECIMAL
- 整數(shù) : SIGNED
- 無符號整數(shù) : 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();
總結(jié)
到此這篇關(guān)于mysql批量新增和存儲的文章就介紹到這了,更多相關(guān)mysql批量新增存儲內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。