今天开始学习mysql,我觉得得先从mysql的编程开始学起,我得记录一下我遇到的值得记录下的东西。
首先就是loop,今天就会记录一下我学习的一些东西。
1.if语句,这个很简单,举一个小例子。
CREATE DEFINER=`root`@`%` PROCEDURE `Csong_1230_01`(i int(10))
BEGIN
set @x = i;
if @x > 10 then
select @x + 10000;
else
select @x + 20000;
end if;
END;
2.repeat感觉有点像oracle里面的loop,需要用until退出,有点像oracle的loop中的exit.
CREATE DEFINER=`root`@`%` PROCEDURE `Csong_1230_02`()
BEGIN
set @x = 0;
repeat
set @x = @x + 1;
until @x = 10 end repeat;
select @x;
END;
3.while倒向传统的while:
CREATE DEFINER=`root`@`%` PROCEDURE `Csong_1230_03`()
begin
set @x = 0;
while @x < 10 do
set @x = @x + 1;
end while;
select @x;
end;
4.loop倒不像oracle的loop,据我学习好像mysql的loop还必须加个leave才能退出,不知道是不是真的,我还得研究一下:
CREATE DEFINER=`root`@`%` PROCEDURE `Csong_1230_04`()
begin
set @x = 0;
fir:
loop
set @x = @x + 1;
if @x = 5 then
leave fir;
end if;
end loop fir;
select @x;
end;
5.iterate优点像continue:
CREATE DEFINER=`root`@`%` PROCEDURE `Csong_1230_05`()
begin
set @x = 0;
fir:
loop
set @x = @x + 1;
if @x = 5 then
iterate fir;
end if;
end loop fir;
select @x;
end;
</pre><pre name="code" class="sql">CREATE DEFINER=`root`@`%` PROCEDURE `Csong_1230_06`()
begin
set @x = 0 ,@y = 0;
fir:
loop
set @x = @x + 1;
if @x = 5 then
set @y = 20;
iterate fir;
end if;
if @x = 10 then
leave fir;
end if;
end loop fir;
select @x,@y;
end;
其实还是挺简单。