欢迎投稿

今日深度:

Mysql 控制结构初识,基本控制结构

Mysql 控制结构初识,基本控制结构


Mysql 流程控制

认识

从我目前所接触的编程语言,C, R, VB, Python, Javascript...,来看, 无非就是变量, 表达式, 流程控制(顺序, 分支, 循环), 封装了一些更高级的数据结构而已, 区别在于应用场景和语言特性, 其实逻辑都是相同的, 唯手熟尔.

选择结构 if-else; case

-- if-esle 语法
IF search_condition THEN 
    statement_list;
[ELSEIF search_condition THEN
    statement_list; ....]
ELSE
    statement_list;
END IF;
-- CASE 语法
CASE case_value
    WHEN when_value THEN statement_list
    [WHEN when_value THEN statement_list]...
    [ELSE statement_list]
END CASE;

OR:
CASE
WHEN search_condition THEN statement_list
[WHEN search_condition THEN statement_list] ...
[ELSE statement_list]
END CASE
-- 随机推送一句表白
drop procedure if exists sayLove;
delimiter //
create procedure sayLove()
begin
    declare num int default 0;
    -- 生成一个1-5间的随机数
    set num := round(rand()*5);
    -- 判断
    case num
        when 1 then select "人生若只如初见";
        when 2 then select "春风十里不如你";
        when 3 then select "爱你就像爱生命";
        else
            select "今晚的月色真美";
    end case;
end //
delimiter ;

call sayLove();

-- out
mysql> call sayLove();
+----------------+
| 今晚的月色真美 |
+----------------+
| 今晚的月色真美 |
+----------------+
1 row in set (0.09 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> call sayLove();
+----------------+
| 爱你就像爱生命 |
+----------------+
| 爱你就像爱生命 |
+----------------+
1 row in set (0.14 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> call sayLove();
+----------------+
| 春风十里不如你 |
+----------------+
| 春风十里不如你 |
+----------------+
1 row in set (0.11 sec)

CASE 能实现的, IF-ELSE也完全能, 只是提供了更多的选择而已.

-- 用 if-esle实现
drop procedure if exists sayLove;
delimiter //
create procedure sayLove()
begin
    declare num int default 0;
    -- 生成一个1-5间的随机数
    set num := round(rand()*5);
    -- 判断
    if num=1 then select "人生若只如初见";
    elseif num=2 then select "春风十里不如你";
    elseif num-3 then select "爱你就像爱生命";
    else
        select "今晚的月色真美";
    end if;
end //
delimiter ;

call sayLove();

MySql 循环

  • WHILE ... DO ... END WHILE 叫什么"当"型循环, 满足条件才进入循环体
  • LOOP ... LEAVE...END LOOP "直到型循环"
  • REPEAT ... UNTIL ... END REPEAT

while ...do ...循环

while search_condition do
    statement_list;
end while;

repeat ...until ...循环

repeat
    statement_list;
until search_condition;
end repeat;

loop ...leave 循环

[begin_label:] loop
    statement_list;
    leave [begin_label];
end loop [end_label];

循环-案例 1+2+...n

-- while 实现 求1+2+3+..n和
-- 自己容易混的点: 忘在结尾; end; 变量忘了 set;
-- 传入参数: in 传入值; out: 传入变量去接收返回的值; inout 传入又输出
drop procedure if exists sumN_while;
delimiter //
create procedure sumN_while(in n int)
begin
    declare total int default 0;
    declare i int default 0;
    -- while ...do ....
    while i <= n do
        set total := total + i;
        set i := i + 1;
    -- 打印一下结果
    end while;
    select concat("1+2+..", n, "的和是:", total) as '输出啦';
end //
delimiter ;

call sumN_while(100);

-- out
call sumN_while(100);
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

+----------------------+
| 输出啦               |
+----------------------+
| 1+2+..100的和是:5050 |
+----------------------+
1 row in set (0.10 sec)

同样同 repeat ... until ..实现, 顺便练习下 out 类型参数

-- repeat 实现 1+2+..n的和
drop procedure if exists sumN_repeat;
delimiter //
-- 设置传入out型参数变量, 用来接收输出值
create procedure sumN_repeat(out total int)
begin
    xxxx
end //
delimiter ;
drop procedure if exists sumN_repeat;
delimiter //
-- 设置再传入out型参数变量, 用来接收输出值
create procedure sumN_repeat(in n int)
begin
    declare i int default 0;
    declare total int default 0;
    
    -- repeat ... until ...
    repeat
        set total := total + i;
        set i := i + 1;
        -- 退出条件 until..True时才退出哦, 注意跟while的区别
        until i > n
    end repeat;
    -- 在内部打印出结果
    select total;
end //
delimiter ;

-- out
call sumN_repeat(100);
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

+-------+
| total |
+-------+
|  5050 |
+-------+
1 row in set (0.09 sec)

用out类型参数.

-- repeat 实现 1+2+..n的和
drop procedure if exists sumN_repeat;
delimiter //
-- 设置再传入out型参数变量, 用来接收输出值
create procedure sumN_repeat(in n int, out total int)
begin
    declare i int default 0;
    
    set total := 0;  -- 顺序: 先decalre 再是set, 不能乱,兄弟
    -- repeat ... until ...
    repeat
        set total := total + i;
        set i := i + 1;
        -- 退出条件 until..注意是条件为True时退出哦
        until i > n
    end repeat;
end //
delimiter ;

-- set @ret := 0;
-- call sumN_repeat(100, @ret);
-- select @ret;

-- out
mysql> set @ret := 0;  -- 这个全局变量 @ret 用来接收过程的 total值哦 
Query OK, 0 rows affected (0.00 sec)

mysql> call sumN_repeat(10000, @ret);
Query OK, 0 rows affected (0.04 sec)

mysql> select @ret;
+----------+
| @ret     |
+----------+
| 50005000 |
+----------+
1 row in set (0.08 sec)

再用 loop....leave 来整一波

-- loop ...leave ... 来实现 求 1+2+..n 的和
drop procedure if exists sumN_loop;
delimiter //
create procedure sumN_loop(in n int, out total int)
begin
    declare i int default 0;
    set total := 0;
    -- loop, 先取一个标签名, 再写退出条件, if-then...
    myLoop: loop
        if i > n then
            leave myLoop;
        end if;
        set total := total + i;
        set i := i + 1;
    end loop;
end //
delimiter ;

-- out
mysql> set @ret := 0;
Query OK, 0 rows affected (0.00 sec)

mysql> call sumN_loop(100, @ret);
Query OK, 0 rows affected (0.00 sec)

mysql> select @ret;
+------+
| @ret |
+------+
| 5050 |
+------+
1 row in set (0.11 sec)

小结MySql控制流

补充: 存储过程的参数声明

  • in 类型: 要求在调用的时候, 接收从外界传入一个值.
  • out 类型: 要求在调用时, 传入一个变量去接收procedure的"返回值"
  • inout 类型: 输入输出型

补充: MySql变量定义

  • 在存储过程内, 用: declare 变量名 类型 [default 值]; 类似"局部变量"
  • 在外边运行, 用: @变量 := 值; 类似"全局变量", 注意MySql的标准赋值符号是 " := ", 而 "=" 只有在update 和set时表示赋值, 其余场景都是 "等号".
  • 选择结构(if, case):
    • if - elseif- esle -end if;
    • case value when value1 then ; when value2 ...then .. else .. . end case;
  • 循环结构(while, repeat, loop)
    • while .... do .... end while;
    • repeat ... until .... end repeat;
    • myLoop: loop ..... if ... then leave myLoop; end if ; ..... end loop;

www.htsjk.Com true http://www.htsjk.com/Mysql/37004.html NewsArticle Mysql 控制结构初识,基本控制结构 Mysql 流程控制 认识 从我目前所接触的编程语言,C, R, VB, Python, Javascript...,来看, 无非就是 变量, 表达式, 流程控制(顺序, 分支, 循环), 封装了一些更高级...
相关文章
    暂无相关文章
评论暂时关闭