zoukankan      html  css  js  c++  java
  • mysql之流程控制

    目录

    • 分支结构
    • 循环结构

    分支结构:

      1.if condition then [statement] elseif condition then [statement] else [statement]  end if

       实例:求两个数中的最大值,如果两个数据相等,则返回0,不相等,则返回最大值  

    delimiter //  -- 把默认的结束符;替换成//
    create function test_if(a int,b int) returns int 
        BEGIN    
        DECLARE max int DEFAULT 0;
      -- 下面为分支结构
    if a > b then set max = a; ELSEIF a<b then set max = b; ELSE set max = 0; end if; return max; end; // -- 查询 select test_if(1,2)

      2.case when condition then [statement] when condition then [staement] else [statement] end case

       实例:求两个数中的最大值,如果两个数据相等,则返回0,不相等,则返回最大值

    delimiter //  -- 把默认的结束符;替换成
    create function test_when(a int ,b int) -- 函数结构
        RETURNS int -- 返回值类型
    BEGIN    -- 开始标签
    DECLARE max int default 0; -- 定义内部变量表示最大值,默认为0
    -- 下面为分支结构
    CASE
    WHEN a > b then 
    SET max = a;
    WHEN a < b then 
    SET max = b;
    ELSE 
    SET max = 0;
    END CASE;
    RETURN max;
    END;
    //
    -- 查询
    select test_when(1,3)

    循环结构:

      循环控制语句包括 while condtion do [statement]  、loop 、repeat  [statemnet] until condtion,其中while、loop满足条件开始循环,至少循环0次;而repeat满足条件退出循环,至少循环1次。

      1. while condtion do [statement] 

      实例:求和

    delimiter //  -- 把默认的结束符;替换成
    -- 求数值a以内的数据之和
    create function test_while(a int) -- 函数结构 
    returns int -- 返回值类型
    begin 
        DECLARE sum int default 0; --
        DECLARE count int default 1;-- 循环次数变量
        -- 下面是循环控制语句
        la:while count <= a do 
            set sum = sum + count;
            set count = count + 1;
        end while la;
    return sum;
    end;
    //

      2.loop 

      实例:求和

    delimiter //  -- 把默认的结束符;替换成
    create function demo_loop(a int) returns int
        BEGIN
        DECLARE s int DEFAULT 0;
        DECLARE i int DEFAULT 1;
        la:loop
        IF i>a then 
        LEAVE la;
        end if;
        set s = s+i;
        set i=i+1;
        end loop la;
        return s;
        end;
    //
    --查询
    select demo_loop(1)

      3.repeat [statement] until condtion

      实例:求和

    delimiter // -- 把默认的结束符;替换成//
    create function demo_repeat(a int) returns int 
        begin 
        DECLARE    s int default 0;
        DECLARE i int DEFAULT 1;
        la:REPEAT
        set s = s+i;
        set i=i+1;
        UNTIL i>a
        end REPEAT la;
        return s;
        end; 
    //
    --查询
    select demo_repeat(10)
  • 相关阅读:
    [转]C#、VB.NET使用HttpWebRequest访问https地址(SSL)的实现
    C#设置System.Net.ServicePointManager.DefaultConnectionLimit,突破Http协议的并发连接数限制
    [转]WebBrowser控件禁用超链接转向、脚本错误提示、默认右键菜单和快捷键
    [转]C#打印DataGridView的例子源码
    c# TreeView 父节点选中/不选时子节点都同步选中/不选
    C#中PictureBox异步加载图片
    [转]FusionCharts 3.1 破解版 – 非常好用的Flash图表控件
    配合JavaScript拖动页面中控件
    在ThinkPad T400上安装win2003 所遇问题
    C# 抛弃MoveTo来实现文件重命名
  • 原文地址:https://www.cnblogs.com/wqk66/p/10858590.html
Copyright © 2011-2022 走看看