(13) Process Control in mysql

Source: Internet
Author: User

(13) Process Control in mysql
Overview

In mysql, you can use the if/case/loop/leave/iterate/repeat/while statement to control the process.

If statement

The if statement is similar to the if statement in the advanced language (c/c ++/php/java.

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 implements branch selection that is more complex than if, similar to switch statements in advanced languages.

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, and iterate statements Loop practice is a simple loop. Other statements are required to exit the loop condition. Similar to loop in assembly, it is similar to a goto statement in advanced languages.
# Loop syntax [begin_label:] loopstatement_listend loop [end_label]
Leave statements are used to jump out of the process structure, similar to break statements in advanced languages. The iterate statement is used to skip the remaining statements of the current loop and directly enter the next loop. Similar to the continue statement in advanced languages.

Example

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

In the preceding example, the variable x is added to the loop by 1. When x = 10, the loop is exited. When the modulus of x 2 is not 0, the variable x2 is added with 1.

Repeat statement

The repeat statement is a conditional loop control statement, similar to the do... while statement in advanced languages.

[begin_lable:] repeat statement_listuntil search_conditionend repeat [end_lable]

Example

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

The while statement is a conditional loop control statement, similar to the while statement in advanced languages.

[begin_label:] while search_condition do statement_listend while [end_label]

Example

set @x=10;while @x1<0 do set @x = @x-1;end while;

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.