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