当前位置: 移动技术网 > IT编程>数据库>Mysql > Mysql 控制结构初识

Mysql 控制结构初识

2019年10月04日  | 移动技术网IT编程  | 我要评论

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;

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网