Home Database Mysql Tutorial mysql while,loop,repeat循环,符合条件跳出循环_MySQL

mysql while,loop,repeat循环,符合条件跳出循环_MySQL

May 27, 2016 pm 01:46 PM
loop while

mysql while,loop,repeat循环,符合条件跳出循环_MySQL

1、while循环

 

DELIMITER $$
DROP PROCEDURE IF EXISTS `sp_test_while`$$
CREATE  PROCEDURE `sp_test_while`(
 IN p_number INT, #要循环的次数
 IN p_startid INT #循环的其实值
)
BEGIN  
DECLARE v_val INT DEFAULT 0;  
SET v_val=p_startid;
outer_label:  BEGIN  #设置一个标记
WHILE v_val<=p_number DO  
    SET v_val=v_val+1;  
    IF(v_val=100)THEN
        LEAVE  outer_label;  #满足条件,终止循环,跳转到end outer_label标记
    END IF;
END WHILE;  
SELECT &lsquo;我是while外,outer_label内的SQL&lsquo;; 
#由于这句SQL在outer_label代码块内,所以level后,这句SQL将不会执行;
#只要是在outer_label代码块内 任意位置 Leave outer_label,那么Leave后的代码将不再执行
END outer_label;   
 SELECT CONCAT(&lsquo;test&lsquo;,v_val) AS tname;
END$$

DELIMITER ;

CALL sp_test_while(1000,0);
Copy after login

2、loop 循环

DELIMITER $$

DROP PROCEDURE IF EXISTS `sp_testloop`$$
CREATE  PROCEDURE `sp_testloop`(
 IN p_number INT, #要循环的次数
 IN p_startid INT #循环的其实值
)
BEGIN  
    DECLARE v_val INT DEFAULT 0;  
    SET v_val=p_startid;

    loop_label:  LOOP   #循环开始
        SET v_val=v_val+1; 
         
        IF(v_val>p_number)THEN
            LEAVE  loop_label;  #终止循环
        END IF;
    END LOOP; 
     SELECT CONCAT(&lsquo;testloop_&lsquo;,v_val) AS tname;
END$$

delimiter ;

CALL sp_testloop(1000,0);
Copy after login

3、repeat循环下载地址

DELIMITER $$
DROP PROCEDURE IF EXISTS `sp_test_repeat`$$
CREATE  PROCEDURE `sp_test_repeat`(
 IN p_number INT, #要循环的次数
 IN p_startid INT #循环的其实值
)
BEGIN  
DECLARE v_val INT DEFAULT 0;  
SET v_val=p_startid;

 REPEAT  #repeat循环开始

    SET v_val=v_val+1; 
      until v_val>p_number #终止循环的条件,注意这里不能使用&lsquo;;&lsquo;分号,否则报错     
      
 END repeat; #循环结束 

 SELECT CONCAT(&lsquo;test&lsquo;,v_val) AS tname;
END$$

DELIMITER ;

CALL sp_test_repeat(1000,0);
Copy after login

 

以上就是mysql while,loop,repeat循环,符合条件跳出循环_MySQL的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

In C language, what is the difference between while(1) and while(0)? In C language, what is the difference between while(1) and while(0)? Aug 31, 2023 am 10:45 AM

We know that in C language, 'while' keyword is used to define a loop that works based on the condition passed to the loop. Now, since the condition can have two values, true or false, the code inside the while block will be executed repeatedly if the condition is true and will not be executed if the condition is false. Now, by passing parameters to the while loop, we can differentiate between while(1) and while(0) because while(1) is a loop where the condition is always considered true and hence the code inside the block will start executing repeatedly. Furthermore, we can state that it is not 1 that is passed to the loop that makes the condition true, but if any non-zero integer is passed to the while loop, then it will be considered as the true condition, so

How to leverage AI Copilot for collaboration in the Microsoft Loop collaboration platform? How to leverage AI Copilot for collaboration in the Microsoft Loop collaboration platform? Nov 18, 2023 pm 01:33 PM

Microsoft Loop, enhanced with its new feature Copilot, is a modern tool designed to improve the way teams collaborate. It consists of three main parts: components, pages, and workspaces. Components are things like lists or notes that stay updated no matter where you use them, whether in email, documents, or chat. This means you are always working with the latest information. Cycle pages are like digital whiteboards where you put all your components, tasks, and data together. These pages can grow as your project grows, making it easy to keep everything in one place. Workspaces in Loop are shared areas where your team can view and organize everything important to the project, helping everyone

How to download and use Microsoft Loop How to download and use Microsoft Loop May 08, 2023 pm 01:16 PM

Microsoft is ready to give users access to the first preview version of the Loop project. Now, let's learn where to get it, how to install it and how to get the most out of it. Want to learn how to use this software across Office apps and manage tasks? You've come to the right place. What is the Microsoft Cycle? What should we say? You can compare loops to project boards. Here you can see a list of all Loop components and Loop pages, and who is currently working on them. Think of it as a modern file explorer where everything is live and collaborative. The loop page is a separate canvas where people can share and collaborate on loop components. In addition, Loop components are constantly updated and edited, without

Usage of while Usage of while Sep 25, 2023 am 09:47 AM

The usage of while is "while condition: code block". The condition is an expression. When the condition is true, the code block is executed, and then it is judged again whether the condition is true. If it is true, the code block continues to be executed until the condition is false. . while is a commonly used loop control statement, used to repeatedly execute a block of code when certain conditions are met.

Is while a keyword in go language? Is while a keyword in go language? Jun 04, 2021 pm 05:01 PM

In the Go language, while is not a keyword. You can use the for statement plus break to achieve the effect of a while loop, such as "for {sum++ if sum>10{break}else{...}}". The go language has 25 keywords such as break, default, func, select, case, defer, go, map, else, goto, for, if, var, etc.

What are the common flow control structures in Python? What are the common flow control structures in Python? Jan 20, 2024 am 08:17 AM

What are the common flow control structures in Python? In Python, the flow control structure is an important tool used to determine the execution order of the program. They allow us to execute different blocks of code based on different conditions, or to execute a block of code repeatedly. The following will introduce common process control structures in Python and provide corresponding code examples. Conditional statements (if-else): Conditional statements allow us to execute different blocks of code based on different conditions. Its basic syntax is: if condition 1: #when condition

How to use the while statement in java How to use the while statement in java Apr 19, 2023 am 09:28 AM

Note 1. The Chinese meaning of the while keyword is when..., that is, when the condition is established, the corresponding code is executed in a loop. The while statement is the basic structure in the loop statement, and the syntax format is relatively simple. Execution process 2. When executing a while statement, first determine the loop condition. When the loop condition is false, the subsequent code of the while statement is directly executed. When the loop condition is true, the loop body code is executed and the loop condition is determined until the loop condition is not established. Example inti=1;intsum=0;while(i

Detailed explanation of the function and usage of break keyword in PHP Detailed explanation of the function and usage of break keyword in PHP Jun 28, 2023 pm 06:39 PM

Detailed explanation of the role and usage of the break keyword in PHP In PHP programming, break is a control flow statement used to interrupt the current loop or switch statement and jump out of the loop or switch. This article will introduce in detail the role and usage of the break keyword. 1. Break in a loop In a loop structure, the function of break is to terminate the loop early and jump out of the loop body to execute the code after the loop. Common loop structures include for, while and do...while. in for loop

See all articles