Get the App
SLTechnology News&Howtos  ›  Database  › 

How to write the for loop statement of mysql

Shulou Source: shulou.com Published: 2022-06-01 03:42:55 09月30日 Update

How to write the for loop statement of mysql? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

How to write the for loop statement of mysql

MySQL does not support for loop statements. MySQL supports while loop, repeat loop and loop loop.

1.while cycle

Delimiter / / # defines the identifier as a double slash drop procedure if exists test; # if there is a test stored procedure, delete create procedure test () # to create a no-parameter stored procedure named test begin declare i int; # declare the variable set I = 0 # variable assignment while I

< 10 do #结束循环的条件: 当i大于10时跳出while循环 insert into test values (i); #往test表添加数据 set i = i + 1; #循环一次,i加一 end while; #结束while循环 select * from test; #查看test表数据 end // #结束定义语句 call test(); #调用存储过程 2.repeat循环 delimiter // #定义标识符为双斜杠 drop procedure if exists test; #如果存在test存储过程则删除 create procedure test() #创建无参存储过程,名称为test begin declare i int; #申明变量 set i = 0; #变量赋值 repeat insert into test values (i); #往test表添加数据 set i = i + 1; #循环一次,i加一 until i >

10 end repeat; # conditions for ending the loop: when I is greater than 10:00, jump out of the repeat loop select * from test; # View the test table data end / / # end the definition statement call test (); # call the stored procedure

3.loop cycle

Delimiter / / # defines the identifier as a double slash drop procedure if exists test; # if there is a test stored procedure, delete create procedure test () # to create a no-parameter stored procedure named test begin declare i int; # declare the variable set I = 0 # variable assignment lp: loop # lp is the loop name, and optionally loop is the keyword insert into test values (I); # add data set I = I + 1 to the test table # cycle once, I plus one if I > 10 then # conditions for ending the cycle: when I is greater than 10:00, jump out of the loop loop leave lp; end if; end loop; select * from test # View test table data end / / # end definition statement call test (); # call stored procedure

MySQL is a relational database management system developed by the Swedish company MySQL AB and belongs to the products of Oracle. MySQL is one of the most popular relational database management systems. In the aspect of WEB application, MySQL is one of the best RDBMS (Relational Database Management System) application software. MySQL is a relational database management system in which relational databases store data in different tables instead of all data in one large warehouse, which increases speed and flexibility.

After reading the above, have you mastered how to write the for loop statement of mysql? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Loops data procedures storage statements variables databases management systems systems management names slashes conditions identifiers identifiers plus one content methods more questions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MySQL Shulou Information Xiaomi Huawei