MySql中三种循环while、loop、repeat的使用

  1. while循环 语法:WHILE [条件] DO [逻辑] END WHILE;
delimiter $$  
create trigger tushu_after_delete 
after delete on sa.tushu
for each row
begin 
while isbn!=null do
set mulu.qit=mulu.qit-1;
end while;
end; $$
delimiter  ;
  1. repeat循环:repeat [逻辑] until [条件判断] end repeat; 注意:until判断末尾不要加分号 ;
delimiter $$  
create trigger tushu_after_delete 
after delete on sa.tushu
for each row
begin 
	repeat     	
	set qit = qit-1 where isbn=sa.mulu.isbn;
    until mulu.isbn=0 
    end repeat;
end; $$
delimiter  ;
  1. loop循环:注意 loop 一般要和一个标签一起使用,且在 loop 循环中一定要有一个判断条件,能够满足在一定的条件下跳出 loop 循环(即 leave )!
delimiter $$  
create trigger tushu_after_delete 
after delete on sa.tushu
for each row
begin 
declare qit int;		#declare声明一个变量
	label : loop
    set mulu.qit = mulu.qit-1 ;if tushu.isbn=mulu.isbn then leave label;
    end if;
    end loop label;
end; $$
delimiter  ;

你可能感兴趣的:(数据库)