更过MySQL使用操作,点击查看专栏: MySQL数据库_命令详解
1.if判断语句
语法:
if 条件 then
#SQL语句
elseif 条件 then
#SQL语句
elseif 条件 then
#SQL语句
....
else
#SQL语句
end if;
- 创建存储过程(p1)传递一个整数参数判断大于90分优秀,反之不优秀
drop procedure if exists p1;
delimiter //
create procedure p1(score int)
begin
if score > 90 then
select '优秀';
else
select ' 不优秀';
end if;
end //
delimiter ;
call p1(99);
call p1(60);
2.while循环语句
语法:
while 条件 do
#SQL语句
end while;
- 输出1-100的和
drop procedure if exists p2;
delimiter //
create procedure p2()
begin
#声明变量i,递增
declare i int default 1;
declare sum int default 0;
while i<=100 do
#累计求和
set sum = sum + i;
#变量更新
set i = i + 1;
end while;
#显示结果
select sum;
end //
delimiter ;
call p2();
+------+
| sum |
+------+
| 5050 |
+------+
1 row in set (0.00 sec)