mysql 循環(huán)insert方式
mysql 循環(huán)insert
親測(cè)成功!可用,復(fù)制即可
DELIMITER ;; CREATE PROCEDURE test_insert() BEGIN DECLARE y TINYINT DEFAULT 1; WHILE y<10 DO INSERT INTO sysuser_user_deposit_log(log_id, type, user_id, operator, fee, message, logtime, deposit) VALUES (NULL, 'expense', '4903', 'system', '0.500', '用戶抽獎(jiǎng),抽獎(jiǎng)單號(hào):1807261600465829', '1532592017', NULL); SET y=y+1; END WHILE ; commit; END;; CALL test_insert();
mysql 循環(huán)語(yǔ)句
本文總結(jié)了mysql常見(jiàn)的三種循環(huán)方式:while、repeat和loop循環(huán)。還有一種goto,不推薦使用。
一、while循環(huán)
delimiter // ? ? ? ? ? ? ? ? ? ? ? ? ? ?#定義標(biāo)識(shí)符為雙斜杠 drop procedure if exists test; ? ? ? ? ?#如果存在test存儲(chǔ)過(guò)程則刪除 create procedure test() ? ? ? ? ? ? ? ? #創(chuàng)建無(wú)參存儲(chǔ)過(guò)程,名稱為test begin ? ? declare i int; ? ? ? ? ? ? ? ? ? ? ?#申明變量 ? ? set i = 0; ? ? ? ? ? ? ? ? ? ? ? ? ?#變量賦值 ? ? while i < 10 do ? ? ? ? ? ? ? ? ? ? #結(jié)束循環(huán)的條件: 當(dāng)i大于10時(shí)跳出while循環(huán) ? ? ? ? insert into test values (i); ? ?#往test表添加數(shù)據(jù) ? ? ? ? set i = i + 1; ? ? ? ? ? ? ? ? ?#循環(huán)一次,i加一 ? ? end while; ? ? ? ? ? ? ? ? ? ? ? ? ?#結(jié)束while循環(huán) ? ? select * from test; ? ? ? ? ? ? ? ? #查看test表數(shù)據(jù) end // ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#結(jié)束定義語(yǔ)句 call test(); ? ? ? ? ? ? ? ? ? ? ? ? ? ?#調(diào)用存儲(chǔ)過(guò)程
二、repeat循環(huán)
delimiter // ? ? ? ? ? ? ? ? ? ? ? ? ? ?#定義標(biāo)識(shí)符為雙斜杠 drop procedure if exists test; ? ? ? ? ?#如果存在test存儲(chǔ)過(guò)程則刪除 create procedure test() ? ? ? ? ? ? ? ? #創(chuàng)建無(wú)參存儲(chǔ)過(guò)程,名稱為test begin ? ? declare i int; ? ? ? ? ? ? ? ? ? ? ?#申明變量 ? ? set i = 0; ? ? ? ? ? ? ? ? ? ? ? ? ?#變量賦值 ? ? repeat ? ? ? ? insert into test values (i); ? ?#往test表添加數(shù)據(jù) ? ? ? ? set i = i + 1; ? ? ? ? ? ? ? ? ?#循環(huán)一次,i加一 ? ? until i > 10 end repeat; ? ? ? ? ? ?#結(jié)束循環(huán)的條件: 當(dāng)i大于10時(shí)跳出repeat循環(huán) ? ? select * from test; ? ? ? ? ? ? ? ? #查看test表數(shù)據(jù) end // ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#結(jié)束定義語(yǔ)句 call test(); ? ? ? ? ? ? ? ? ? ? ? ? ? ?#調(diào)用存儲(chǔ)過(guò)程
三、loop循環(huán)
delimiter // ? ? ? ? ? ? ? ? ? ? ? ? ? ?#定義標(biāo)識(shí)符為雙斜杠 drop procedure if exists test; ? ? ? ? ?#如果存在test存儲(chǔ)過(guò)程則刪除 create procedure test() ? ? ? ? ? ? ? ? #創(chuàng)建無(wú)參存儲(chǔ)過(guò)程,名稱為test begin ? ? declare i int; ? ? ? ? ? ? ? ? ? ? ?#申明變量 ? ? set i = 0; ? ? ? ? ? ? ? ? ? ? ? ? ?#變量賦值 ? ? lp : loop ? ? ? ? ? ? ? ? ? ? ? ? ? #lp為循環(huán)體名,可隨意 loop為關(guān)鍵字 ? ? ? ? insert into test values (i); ? ?#往test表添加數(shù)據(jù) ? ? ? ? set i = i + 1; ? ? ? ? ? ? ? ? ?#循環(huán)一次,i加一 ? ? ? ? if i > 10 then ? ? ? ? ? ? ? ? ?#結(jié)束循環(huán)的條件: 當(dāng)i大于10時(shí)跳出loop循環(huán) ? ? ? ? ? ? leave lp; ? ? ? ? end if;? ? ? end loop; ? ? select * from test; ? ? ? ? ? ? ? ? #查看test表數(shù)據(jù) end // ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#結(jié)束定義語(yǔ)句 call test(); ? ? ? ? ? ? ? ? ? ? ? ? ? ?#調(diào)用存儲(chǔ)過(guò)程
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
MySQL查詢優(yōu)化:連接查詢排序limit(join、order by、limit語(yǔ)句)介紹
兩張表連接查詢并limit,SQL效率很高,但是加上order by以后,語(yǔ)句的執(zhí)行時(shí)間變的巨長(zhǎng),效率巨低,接下來(lái)為大家介紹下連接查詢排序limit2013-04-04mysql 數(shù)據(jù)庫(kù)備份和還原方法集錦 推薦
本文討論 MySQL 的備份和恢復(fù)機(jī)制,以及如何維護(hù)數(shù)據(jù)表,包括最主要的兩種表類型:MyISAM 和 Innodb,文中設(shè)計(jì)的 MySQL 版本為 5.0.22。2010-03-03Mysql 5.7從節(jié)點(diǎn)配置多線程主從復(fù)制的方法詳解
這篇文章主要介紹了Mysql 5.7從節(jié)點(diǎn)配置多線程主從復(fù)制的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-03-03介紹一個(gè)針對(duì)C++程序的MySQL訪問(wèn)庫(kù)soci
這篇文章主要介紹了介紹一個(gè)針對(duì)C++程序的MySQL訪問(wèn)庫(kù)soci,文章中還講了其中的一些操作方法,需要的朋友可以參考下2015-05-05