簡體   English   中英

如何在MySQL中的函數內使用Delete語句

[英]How to use delete statement inside a function in 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-您正在創建一個函數,因此它需要一個returns聲明

2-您的deletes命令語法錯誤, limit應該是最后一個子句(之間,盡管可以使用,但在delete中沒有任何限制)

3 -沒有THENELSE語句,也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 ;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM