Mysql process control
understanding
Programming language from my current contact, C, R, VB, Python , Javascript ..., view, nothing more variables, expressions, flow control (sequential, branch, loop), encapsulating some of the more advanced data structures nothing more , except that the language features and application scenarios, in fact, the logic is the same, only hand-cooked Seoul.
Selection structure 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 achievable, IF-ELSE fully able, but it offers more choice.
-- 用 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 cycle
- WHILE ... DO ... END WHILE what "when the" cycle type, the condition before entering the loop
- LOOP ... LEAVE ... END LOOP "until circulating"
- REPEAT ... UNTIL ... END REPEAT
while ... do ... loop
while search_condition do
statement_list;
end while;
repeat ... until ... loop
repeat
statement_list;
until search_condition;
end repeat;
loop ... leave cycle
[begin_label:] loop
statement_list;
leave [begin_label];
end loop [end_label];
Cycle - Case 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)
Also with repeat ... until .. realize, the way out to practice under the type parameter
-- 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 with the type parameters.
-- 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)
Then loop .... leave to the whole wave
-- 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)
Summary MySql control flow
Added: stored procedure parameter declaration
- in Type: Request call when receiving a pass from the outside world value.
- out Type: Request When calling, passing a variable to receive the procedure of "return value"
- inout type: input-output type
Supplementary: MySql variable definitions
- In the storage process, with: declare variable name type [default value]; similarly "local variables"
- Outside run, use: @ variable: = value; similar to the "global variables", pay attention to MySql is a standard assignment symbol ": =", and "=" represents assignment only in the update and set, the rest of the scenes are "equal sign . "
- Selection structure (if, case):
- if - elseif- esle -end if;
- case value when value1 then ; when value2 ...then .. else .. . end case;
- Loop structure (while, repeat, loop)
- while .... do .... end while;
- repeat ... until .... end repeat;
- myLoop: loop ..... if ... then leave myLoop; end if ; ..... end loop;