In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail what the mysql process control statement is. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
The process control statements in mysql include: IF statement, CASE statement, LOOP statement, WHILE statement, REPEAT statement, LEAVE statement and ITERATE statement, all of which can control the flow of the program.
Process control statements can be used to control the flow of a program in stored procedures and custom functions. The process control statements in MySQL include: IF statement, CASE statement, LOOP statement, WHILE statement, REPEAT statement, LEAVE statement and ITERATE statement, which can be used for process control.
1. IF statement
IF statements are used to judge conditions and perform different operations according to different conditions. When the statement is executed, it first judges whether the condition after IF is true, then executes the statement after THEN. If it is false, it continues to judge the IF statement until it is true. When the above is not satisfied, the content after the ELSE statement is executed. The IF statement is expressed as follows:
IF condition THEN... ELSE condition THEN... ELSE... END IF
Example: use an if statement to determine a condition.
-- create stored procedure CREATE PROCEDURE example_if (IN x INT) BEGIN IF x = 1 THEN SELECT 1; ELSEIF x = 2 THEN SELECT 2; ELSE SELECT 3; END IF;END;-- call stored procedure CALL example_if (2)
Note: the IF () function in MySQL is different from the IF statement here.
2. CASE statement
The CASE statement is a multi-branch statement structure, which first looks for a value equal to the VALUE after CASE from the VALUE after WHEN, and executes the content of the branch if it is found, otherwise the content after ELSE is executed. The CASE statement is expressed as follows:
CASE value WHEN value THEN... WHEN value THEN... ELSE... END CASE
Another syntax representation of the CASE statement is as follows:
CASE WHEN value THEN... WHEN value THEN... ELSE... END CASE
Example: use a case statement to determine a condition.
-- create stored procedure CREATE PROCEDURE example_case (IN x INT) BEGIN CASE x WHEN 1 THEN SELECT 1; WHEN 2 THEN SELECT 2; ELSE SELECT 3; END CASE;END;-- call stored procedure CALL example_case (5)
3. WHILE loop statement
When the WHILE loop statement is executed, it first determines whether the condition condition is true, if so, executes the loop body, otherwise exits the loop. The syntax is expressed as follows:
WHILE condition DO...END WHILE
Example: use a WHILE loop statement to execute the sum of the top 100.
-- create the stored procedure CREATE PROCEDURE example_while (OUT sum INT) BEGIN DECLARE i INT DEFAULT 1; DECLARE s INT DEFAULT 0; WHILE I 100 THEN-- exit the LOOP loop LEAVE loop_label; END IF; END LOOP; SET sum = sum sum-call the stored procedure CALL example_loop (@ sum) SELECT @ sum
5. REPEAT loop statement
The REPEAT loop statement executes the loop body once, then determines whether the condition condition is true, then exits the loop, otherwise it continues to execute the loop. The REPEAT statement is expressed as follows:
REPEAT... UNTIL conditionEND REPEAT
Example: use a REPEAT loop statement to find the sum of the top 100.
-- create the stored procedure CREATE PROCEDURE example_repeat (OUT sum INT) BEGIN DECLARE i INT DEFAULT 1; DECLARE s INT DEFAULT 0; REPEAT SET s = SET I; UNTIL I > 100 END REPEAT; SET sum = alternate END REPEAT; SET sum-call stored procedure CALL example_repeat (@ sum); SELECT @ sum
6. ITERATE statement
ITERATE statements can appear within LOOP, REPEAT, and WHILE statements, meaning "loop again". The statement format is as follows:
ITERATE label
The format of this statement is more or less the same as that of LEAVE, except that the LEAVE statement leaves a loop, while the ITERATE statement restarts a loop.
Example: find the sum of odd values within 10.
-- create the stored procedure CREATE PROCEDURE example_iterate (OUT sum INT) BEGIN DECLARE i INT DEFAULT 0; DECLARE s INT DEFAULT 0; loop_label:LOOP SET I = iTun1; IF I > 10 THEN-- exit the entire loop LEAVE loop_label END IF; IF (I mod 2) THEN SET s = Signori; ELSE-- exit this cycle and continue to the next cycle, ITERATE loop_label; END IF; END LOOP; SET sum = SignEND. -- call the stored procedure CALL example_iterate (@ sum); SELECT @ sum about what the mysql flow control statement is. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.