(13)mysql中的流程式控制制,13mysql流程式控制制

來源:互聯網
上載者:User

(13)mysql中的流程式控制制,13mysql流程式控制制
概述

在mysql中可以使用if/case/loop/leave/iterate/repeat/while語句進行流程式控制制。

if語句

if語句實現條件判斷,類似進階語言(c/c++/php/java等)中的if語句。

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

舉例

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語句

case實現比if更複雜的一些分支選擇,類似與進階語言中的 switch語句。

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

舉例

case sid    when 2 then        set @x1 = 2;    else        set @x1 = 1;end case
loop、leave、iterate語句
#loop文法[begin_label:] loopstatement_listend loop [end_label]

舉例

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;

上面的例子,表示變數x迴圈加1,當x=10,退出迴圈。當x取2的模不為0時候,變數x2加1。

repeat語句

repeat語句是有條件的迴圈控制語句,類似與進階語言中的do...while語句。

[begin_lable:] repeat    statement_listuntil search_conditionend repeat [end_lable]

舉例

set @x=0;repeat    set @x = @x +1;until @x>10 end repeat;
while語句

while語句是實現有條件的迴圈控制語句,類似與進階語言中的while語句。

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

舉例

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


流程式控制制在預存程序或函數中的運用,參考連結如下:
(9)mysql中的預存程序和自訂函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.