delimiter //
create function kill_aliens (rand int)
begin
if rand <= 0.2 and rand >= 0 then
delete from alienkilling limit 2 where dtypeid = 11;
elseif rand <=0.75 and rand > 0.2 then
delete from alienkilling limit 3 where dtypeid = 12;
else rand <=1 and rand >0.75 then
delete from alienkilling limit 1 where dtypeid = 13;
end if; //
它给我一个语法错误
答案 0 :(得分:0)
您的函数定义允许列表存在很多问题:
1 - 您正在创建一个函数,因此需要returns
声明
2 - 你的删除命令语法错误,limit
应该是最后一个子句(在删除之间没有任何意义,尽管它有效)
3 - THEN
语句没有ELSE
,else
也没有条件,它应该是另一个ELSEIF
4 - 如果它是一个功能,你需要return
某事
5 - 您没有使用end
声明
所以应该是这样的:
delimiter //
create function kill_aliens (rand int) returns int
begin
if rand <= 0.2 and rand >= 0 then
delete from alienkilling where dtypeid = 11 limit 2;
elseif rand <=0.75 and rand > 0.2 then
delete from alienkilling where dtypeid = 12 limit 3;
elseif rand <=1 and rand >0.75 then
delete from alienkilling where dtypeid = 13 limit 1;
end if;
return 0; -- it must be some value
end //
delimiter ;
附注:了解如何缩进代码。如果这不会返回任何东西,它应该是一个程序而不是一个函数。
类似的东西:
delimiter //
create procedure kill_aliens (rand int)
begin
if rand <= 0.2 and rand >= 0 then
delete from alienkilling where dtypeid = 11 limit 2;
elseif rand <=0.75 and rand > 0.2 then
delete from alienkilling where dtypeid = 12 limit 3;
elseif rand <=1 and rand >0.75 then
delete from alienkilling where dtypeid = 13 limit 1;
end if;
end //
delimiter ;