() Process Control in MySQL

Source: Internet
Author: User

Overview

In MySQL, you can use the If/case/loop/leave/iterate/repeat/while statement for Process Control.

If statement

The IF statement implements the condition judgment,类似高级语言(c/c++/php/java等)中的if语句。

if search_condition then    statement_list[elseif search_condition then statement_list]...[else statement_list]end if

Example

if mobile=‘13911113222‘ and psw=‘720717‘ then    set useridx = 10008888;else if mobile=‘13911113333‘ and psw=‘720717‘ then    set useridx = 10006666;else    select UserID into useridx from User_ID order by rand() limit 1;end if;
Case Statement

Case implementation is more complex than if some branch selection,类似与高级语言中的 switch语句。

case case_value    when when_value then statement_list    [when when_value then statement_list]...    [else statement_list]end case

Example

case sid    when 2 then        set @x1 = 2;    else        set @x1 = 1;end case
loop, leave, iterate statements
    1. Loop practice simple loops, exiting the loop condition requires other statement definitions. Similar to the loop in the Assembly,类似于高级语言中的goto语句。
#loop语法[begin_label:] loopstatement_listend loop [end_label]
    1. The leave statement is used to jump out of the process structure,类似于高级语言中的break语句。
    2. The iterate statement is used to skip the remaining statements of the current loop and go directly to the next round of loops.类似与高级语言中的continue语句。

Example

set @x=0;set @x2=0;ins:loop    set @[email protected]+1;    if @x=10 then        leave ins;    elseif mod(@x,2)=0 then        iterate ins;    end if    set @[email protected]+1;end loop ins;

The above example indicates that the variable x loops plus 1, when x=10, exits the loop. When X takes 2 modulo not 0, the variable x2 plus 1.

Repeat Statements

The repeat statement is a conditional loop control statement,类似与高级语言中的do...while语句。

[begin_lable:] repeat    statement_listuntil search_conditionend repeat [end_lable]

Example

set @x=0;repeat    set @x = @x +1;end repeat;
While statement

The while statement is the implementation of a conditional loop control statement,类似与高级语言中的while语句。

do    statement_listend while [end_label]

Example

set @x=10;do    set @x = @x-1;end while;


The use of process control in stored procedures or functions, refer to the following links:
(9) Stored procedures and custom functions in MySQL

() Process Control in MySQL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.