如何在mysql

时间:2017-04-06 04:57:46

标签: mysql

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; //

它给我一个语法错误

1 个答案:

答案 0 :(得分:0)

您的函数定义允许列表存在很多问题:

1 - 您正在创建一个函数,因此需要returns声明

2 - 你的删除命令语法错误,limit应该是最后一个子句(在删除之间没有任何意义,尽管它有效)

3 - THEN语句没有ELSEelse也没有条件,它应该是另一个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 ;