Distinguish differences and use MySQL WHILE LOOP and REPEAT cycle of MySQL MySQL three loop recycling method
A, MySQL cycle Overview
There are three loop MySQL, namely the WHILE, the REPEAT, LOOP (said to have GOTO), it can not be used alone, mainly for stored procedures and functions FUNCTION in PROCEDURE.
Two, WHILE loop
1、语法: WHILE condition DO doSomething END WHILE ;
2. Description: condition where the condition is satisfied, the contents of the loop is executed, is not satisfied, then the end of the cycle. (Eg: 2> 1, satisfies; 1 <2, is not satisfied.) - - first determination, performed after.
3. Example: Creating a while loop stored procedure pro_while
DROP PROCEDURE IF EXISTS pro_while ;
CREATE PROCEDURE pro_while()
BEGIN
DECLARE sum INT DEFAULT 0 ;
WHILE sum < 100 DO
INSERT INTO `chapter`.`batch` (`id`, `name`, `age`)
VALUES (CONCAT(sum,''),CONCAT('while',sum) , sum);
SET sum = sum + 1;
END WHILE ;
END;
4, call a stored procedure: the CALL pro_while ();
Three, REPEAT loop
1、语法: REPEAT doSomething UNTIL condition END REPEAT ;
2. Description: doSomething to perform related operations, and then determines whether the condition satisfied, the loop ends. (WHILE loop and vice versa) --- first implementation, the determination.
3. Example: Creating a repeat loop function fun_repeat
DROP FUNCTION IF EXISTS fun_repeat ;
CREATE FUNCTION fun_repeat() RETURNS INT
BEGIN
DECLARE sum INT DEFAULT 1000 ;
REPEAT
INSERT INTO `chapter`.`batch` (`id`, `name`, `age`)
VALUES (CONCAT(sum,''),CONCAT('repeat',sum) , sum);
SET sum = sum + 1;
UNTIL sum > 1100 END REPEAT ; -- 满足条件结束循环
RETURN 1;
END;
4, the function performed: the SELECT fun_repeat ();
Four, LOOP cycle
1, grammar:
loop_name : LOOP
IF condition THEN
LEAVE loop_name ;
END IF;
doSomething
END LOOP;
2, Description:
- loop_name for the cycle name, custom, unavailable keywords.
- doSomething related operations performed.
- condition condition is met, the loop ends. --- first determine, after the execution.
3. Example: Creating a loop circulation of the stored procedure pro_loop
DROP PROCEDURE IF EXISTS pro_loop ;
CREATE PROCEDURE pro_loop()
BEGIN
DECLARE sum int DEFAULT 10000 ;
loop_sums : LOOP -- 【开始】loop 名字,自定义
IF sum > 10100 THEN
LEAVE loop_sums ; -- 满足条件,则结束循环
END IF;
INSERT INTO `chapter`.`batch` (`id`, `name`, `age`)
VALUES (CONCAT(sum,''),CONCAT('loop',sum) , sum);
SET sum = sum + 1;
END LOOP ; -- 【结束】
END ;
4, call a stored procedure: the CALL pro_loop ();
V. Summary
1, WHILE loop, the first determination, performed after. To meet the conditions, execution, execution cycle.
2, REPEAT loop, before and after the execution determination. The condition is not performed, the end of the cycle.
3, LOOP cycle, before and after the execution determination. The condition is not performed, the end of the cycle.
4, when the condition is false, REPEAT loop can be performed once, similar to the java do {} while (condition); cyclic; and WHILE, LOOP cycle can not be performed.
5, the above is my own summary, if any mistakes, please correct me, thanks! ^ _ ^.
understand more...
MySQL stored procedures organize learning, using the cursor, input and output parameters
MyBatis call a stored procedure, MyBatis to use function calls
MySQL data compilation to create a trigger and trigger view, delete trigger