drop table if exists t_user; create table t_user( id int primary key comment '编号', sex tinyint not null default 1 comment '性别:1男,2女', name varchar(16) not null default '' comment '姓名' )comment '用户表';
insert into t_user values(1,1,'envy'),(2,1,'book'),(3,2,'movie'),(4,1,'lichee'),(5,2,'bike');
select * from t_user;
当然可能后续还会使用到test1和test2表,因此这里也提前创建了:
1 2 3 4 5
drop table if exists test1; create table test1(a int not null);
drop table if exists test2; create table test2(a int not null,b int not null);
case 表达式 when 值1 then 结果1或者语句1(注意如果是语句则需要添加分号) when 值2 then 结果2或者语句2 when 值3 then 结果3或者语句3 ... else 结果n或者语句n end [case](如果是放在begin end之间需要加case,如果在select后则不需要)
在select中使用
同样是前面那个需求:查询t_user数据表中的数据,要求显示编号、性别(男或者女)、姓名。
这里有两种写法,第一种类似于Java中的if else用法:
1 2
select id as 编号, (case sex when 1 then '男' else '女' end) as 性别,name as 姓名 from t_user;
执行结果如下所示:
1 2 3 4 5 6 7 8 9 10 11 12
mysql> select id as 编号, -> (case sex when 1 then '男' else '女' end) as 性别,name as 姓名 from t_user; +------+------+--------+ | 编号 | 性别 | 姓名 | +------+------+--------+ | 1 | 男 | envy | | 2 | 男 | book | | 3 | 女 | movie | | 4 | 男 | lichee | | 5 | 女 | bike | +------+------+--------+ 5 rows in set (0.00 sec)
第二种类似于Java中的if else if用法:
1 2
select id as 编号, (case sex when 1 then '男' when 2 then '女' end) as 性别,name as 姓名 from t_user;
执行结果如下所示:
1 2 3 4 5 6 7 8 9 10 11 12
mysql> select id as 编号, -> (case sex when 1 then '男' when 2 then '女' end) as 性别,name as 姓名 from t_user; +------+------+--------+ | 编号 | 性别 | 姓名 | +------+------+--------+ | 1 | 男 | envy | | 2 | 男 | book | | 3 | 女 | movie | | 4 | 男 | lichee | | 5 | 女 | bike | +------+------+--------+ 5 rows in set (0.00 sec)
/*删除存储过程proc1*/ drop procedure if exists proc1; /*如果存在id为6的记录就删除*/ delete from t_user where id=6; /*声明结束符为$*/ delimiter $ /*创建存储过程proc1*/ create procedure proc1(id int,sex_str varchar(8),name varchar(16)) begin /*声明变量v_sex用于存放性别*/ declare v_sex tinyint unsigned; /*根据sex_str的值来设置性别*/ case sex_str when '男' then set v_sex = 1; when '女' then set v_sex = 2; end case ; /*插入数据*/ insert into t_user values (id,v_sex,name); end $ /*结束符置为;*/ delimiter ;
第二步,调用存储过程:
1
call proc1(6,'男','nice');
第三步,查询t_user表来验证效果:
1 2 3 4 5 6 7 8 9 10 11 12
mysql> select * from t_user; +----+-----+--------+ | id | sex | name | +----+-----+--------+ | 1 | 1 | envy | | 2 | 1 | book | | 3 | 2 | movie | | 4 | 1 | lichee | | 5 | 2 | bike | | 6 | 1 | nice | +----+-----+--------+ 6 rows in set (0.00 sec)
/*删除函数fun1*/ drop function if exists fun1; /*声明结束符为$*/ delimiter $ /*创建函数fun1*/ create function fun1(sex tinyint unsigned) returns varchar(8) begin /*声明变量v_sex用于存放性别*/ declare v_sex varchar(8); case sex when 1 then set v_sex := '男'; else set v_sex := '女'; end case ; return v_sex; end $ /*结束符置为;*/ delimiter ;
第二步,调用fun1函数并查询t_user表的结果:
1 2 3 4 5 6 7 8 9 10 11 12
mysql> select sex,fun1(sex) as 性别,name from t_user; +-----+------+--------+ | sex | 性别 | name | +-----+------+--------+ | 1 | 男 | envy | | 1 | 男 | book | | 2 | 女 | movie | | 1 | 男 | lichee | | 2 | 女 | bike | | 1 | 男 | nice | +-----+------+--------+ 6 rows in set (0.00 sec)
类似于Java中的多重if语句
此种用法下的case语句的语法格式如下所示:
1 2 3 4 5 6 7
case when 条件1 then 结果1或者语句1(注意如果是语句需要加分号) when 条件2 then 结果2或者语句2 when 条件3 then 结果3或者语句3 ... else 结果n或者语句n end [case] (如果是放在begin end之间需要加case,如果是在select后面case可以省略)
其实这种写法和1中的写法非常相似,因此这里就忽略。
if结构
if结构非常类似于Java中的if…else if…else的语法,使用的语法格式如下所示:
1 2 3 4 5
if 条件语句1 then 语句1; elseif 条件语句2 then 语句2; ... else 语句n; end if;
/*删除id=7的记录*/ delete from t_user where id=7; /*删除存储过程*/ drop procedure if exists proc2; /*声明结束符为$*/ delimiter $ /*创建存储过程*/ create procedure proc2(v_id int,v_sex varchar(8),v_name varchar(16),out result tinyint) begin /*用来保存user记录的数量*/ declare v_count tinyint default 0; /*根据v_id查询数据放入v_count中*/ select count(id) into v_count from t_user where id = v_id; /*v_count>0表示数据存在,则修改,否则新增*/ if v_count>0 then begin declare lsex tinyint; select if(lsex='男',1,2) into lsex; update t_user set sex = lsex,name = v_name where id = v_id; /*获取update影响行数*/ select row_count() into result; end; else begin declare lsex tinyint; select if(lsex='男',1,2) into lsex; insert into t_user values(v_id,lsex,v_name); select 0 into result; end; end if; end $ /*结束符置为;*/ delimiter ;
/*删除test1表记录*/ delete from test1; /*删除存储过程*/ drop procedure if exists proc3; /*声明结束符为$*/ delimiter $ /*创建存储过程*/ create procedure proc3(v_count int) begin declare i int default 1; a:while i<=v_count do insert into test1 values (i); set i=i+1; end while; end $ /*结束符置为;*/ delimiter ;
/*删除test1表记录*/ delete from test1; /*删除存储过程*/ drop procedure if exists proc4; /*声明结束符为$*/ delimiter $ /*创建存储过程*/ create procedure proc4(v_count int) begin declare i int default 1; a:while i<=v_count do insert into test1 values (i); /*判断i是否为10,是的话就离开循环a*/ if i =10 then leave a; end if; set i=i+1; end while; end $ /*结束符置为;*/ delimiter ;
/*删除test1表记录*/ delete from test1; /*删除存储过程*/ drop procedure if exists proc5; /*声明结束符为$*/ delimiter $ /*创建存储过程*/ create procedure proc5(v_count int) begin declare i int default 0; a:while i<=v_count do set i = i+1; /*判断i是否为偶数,否的话就跳过本次循环*/ if i%2 !=0 then iterate a; end if; /*插入数据*/ insert into test1 values (i); end while; end $ /*结束符置为;*/ delimiter ;
/*删除test2表记录*/ delete from test2; /*删除存储过程*/ drop procedure if exists proc7; /*声明结束符为$*/ delimiter $ /*创建存储过程*/ create procedure proc7(v_a_count int,v_b_count int) begin declare v_a int default 0; declare v_b int default 0; a:while v_a<=v_a_count do set v_a = v_a+1; set v_b=0; b:while v_b<=v_b_count do set v_b=v_b+1; if v_a%2!=0 then iterate a; end if; if v_b%2!=0 then iterate b; end if; insert into test2 values (v_a,v_b); end while b; end while a; end $ /*结束符置为;*/ delimiter ;
/*删除test1表记录*/ delete from test1; /*删除存储过程*/ drop procedure if exists proc6; /*声明结束符为$*/ delimiter $ /*创建存储过程*/ create procedure proc6(v_count int) begin declare i int default 1; a:repeat insert into test1 values (i); set i=i+1; until i>v_count end repeat; end $ /*结束符置为;*/ delimiter ;
/*删除test1表记录*/ delete from test1; /*删除存储过程*/ drop procedure if exists proc8; /*声明结束符为$*/ delimiter $ /*创建存储过程*/ create procedure proc8(v_count int) begin declare i int default 0; a:loop set i=i+1; /*当i>v_count的时候退出循环*/ if i>v_count then leave a; end if; insert into test1 values (i); end loop a; end $ /*结束符置为;*/ delimiter ;