[英]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 -沒有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 ;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.