所谓流程控制, 无非就是 分支 和 循环sql
假设有一个教师表, 有
id
,name
,sex
,salary
四个字段(ID,姓名,性别,工资),oop
根据不一样的工资, 分别显示不一样的身份post
输入id, 若是工资大于5000, 输出
土豪
, 若是小于1500, 输出low 逼
,ui
若是1500到5000之间, 输出
马马虎虎
, 若是没有, 则显示''查无此人''spa
drop table if exists teacher;
CREATE TABLE `teacher` (
`id` int(11) NOT NULL auto_increment primary key,
`salary` int(11) NOT NULL,
`sex` tinyint(1) NOT NULL comment '1 for male, 2 for female',
`name` char(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `teacher`
(`id`, `salary`, `sex`, `name`)
VALUES
(1, 1500, 1, '教师1');
INSERT INTO `teacher`
(`id`, `salary`, `sex`, `name`)
VALUES
(2, 5500, 2, '教师2');
INSERT INTO `teacher`
(`id`, `salary`, `sex`, `name`)
VALUES
(3, 6500, 2, '教师3');
INSERT INTO `teacher`
(`id`, `salary`, `sex`, `name`)
VALUES
(4, 7500, 1, '教师4');
INSERT INTO `teacher`
(`id`, `salary`, `sex`, `name`)
VALUES
(5, 8500, 2, '教师5');
复制代码
sql语句以下3d
drop PROCEDURE if exists find_teacher;
create PROCEDURE find_teacher(in tid int,out type varchar(10)) begin
declare teacher_salary int;
select salary into teacher_salary from teacher where id = tid;
IF teacher_salary > 5000 THEN
set type = '土豪';
ELSEif teacher_salary <= 5000 and teacher_salary >= 1500 then
set type = '马马虎虎';
elseif teacher_salary < 1500 then
set type = 'low 逼';
elseif teacher_salary is null then
set type = '查无此人!!!';
END IF;
end;
call find_teacher(6,@type);
select @type;
复制代码
再举一个例子code
成绩大于90, 学霸, 60到90之间, 继续努力, 小于60, 学渣cdn
drop PROCEDURE if exists 评分;
create procedure 评分(in 分数 int)begin
if
分数 >= 90 then select '学霸';
elseif 分数 >= 60 and 分数 < 90 then select '继续加油';
else select '学渣';
end if;
end;
call 评分(97);
复制代码
再再举一个例子blog
等公交, 车来了, 上车, 快来了, 继续等, 其余状况, 走起rem
drop PROCEDURE if exists 等公交;
create procedure 等公交(in 状态 char(10))begin
if
状态 = '来了' then select '上公交';
elseif 状态 = '快来了' then select '等公交';
else select '走起';
end if;
end;
call 等公交('快来了');
复制代码
一样是分支结构, case既能够处理固定的值, 也能够处理范围
原来咱们是用
1
表明男性,2
表明女性, 如今我想在查询的时候, 直接把1
转换成男性, 把2
转变成女性
原来的结果是这样的...
下面开始写sql语句
DROP PROCEDURE IF EXISTS select_teacher;
CREATE PROCEDURE select_teacher ( ) BEGIN
SELECT id, salary,
CASE sex
WHEN 1 THEN '男性'
WHEN 2 THEN '女性'
END AS 'sex',
name FROM teacher;
END;
CALL select_teacher ( );
复制代码
如今咱们使用case, 再来改写一下以前的if else...
DROP PROCEDURE IF EXISTS select_teacher;
CREATE PROCEDURE select_teacher() BEGIN
SELECT id, salary,
CASE sex
WHEN 1 THEN '男性'
WHEN 2 THEN '女性'
END AS 'sex',
CASE
WHEN salary > 5000 THEN '土豪'
WHEN salary >= 1500 AND salary <= 5000 THEN '马马虎虎'
WHEN salary < 1500 THEN 'low 逼'
END AS `type`
FROM teacher;
END;
CALL select_teacher();
复制代码
下面, 咱们使用三种循环, 分别输出10遍"我爱你"
drop PROCEDURE if exists love;
create PROCEDURE love() begin
declare num int default 0;
myloop:loop
set num = num + 1;
if num > 10 then
leave myloop;
end if;
select num;
select '我爱你';
end loop;
end;
call love();
复制代码
drop PROCEDURE if exists love;
create PROCEDURE love() begin
declare num int default 0;
while num < 10 do
set num = num + 1;
select num;
select '我爱你';
end while;
end;
call love();
复制代码
drop PROCEDURE if exists love;
create PROCEDURE love() begin
declare num int default 0;
repeat
set num = num + 1;
select num;
select '我爱你';
until num > 9 end repeat;
end;
call love();
复制代码
我错了
while do
drop procedure if exists 我错了;
create procedure 我错了() begin
declare 计数 int default 0;
while
计数< 10
do
select 计数;
select '我错了...';
set 计数 = 计数 + 1;
end while;
end;
call 我错了();
复制代码
loop
drop procedure if exists 我错了;
create procedure 我错了() begin
declare 计数 int default 0;
hello:loop
select 计数;
select '我错了...';
set 计数 = 计数 + 1;
if 计数 >= 10 then leave hello;
end if;
end loop;
end;
call 我错了();
复制代码
repeat until
drop procedure if exists 我错了;
create procedure 我错了() begin
declare 计数 int default 0;
repeat
select 计数;
select '我错了...';
set 计数 = 计数 + 1;
until
计数 >= 10
end repeat;
end;
call 我错了();
复制代码
至关于 continue 和 break
仅适用于循环(loop
,repeat
,while
)
跳过和中断
循环输出1到11, 逢5过
drop PROCEDURE if exists test;
create PROCEDURE test() begin
declare x int default 0;
myloop:loop
set x = x + 1;
if x % 5 = 0 then ITERATE myloop; end if;
select x;
if x> 10 then leave myloop; end if;
end loop;
end;
call test();
复制代码
使用
while
,repeat
,loop
三种方式, 计算从1加到100
提早告诉你, 结果是
5050
所谓从1加到100, 就是...
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 + 21 + 22 + 23 + 24 + 25 + 26 + 27 + 28 + 29 + 30 + 31 + 32 + 33 + 34 + 35 + 36 + 37 + 38 + 39 + 40 + 41 + 42 + 43 + 44 + 45 + 46 + 47 + 48 + 49 + 50 + 51 + 52 + 53 + 54 + 55 + 56 + 57 + 58 + 59 + 60 + 61 + 62 + 63 + 64 + 65 + 66 + 67 + 68 + 69 + 70 + 71 + 72 + 73 + 74 + 75 + 76 + 77 + 78 + 79 + 80 + 81 + 82 + 83 + 84 + 85 + 86 + 87 + 88 + 89 + 90 + 91 + 92 + 93 + 94 + 95 + 96 + 97 + 98 + 99 + 100
也能够试试从1加到1000...