2012-10-05 29 views
2

我最近開始使用MySQL n,現在我想創建一個觸發器,但是MySQL在我的語法中返回一個錯誤。觸發器內select語句中的MySQL錯誤

delimiter $$; 
create trigger abc after insert on ratings 
for each row 
    begin 
     set @n1 = select avg(rating) from ratings join users where ratings.uname=users.uname 
     and ratings.bookid=new.bookid users.`type`='admin' or users.`type`='critic'; 
     update books set avgcriticrating = @n1 where bookid=new.bookid; 
end; 

select語句在單獨觸發時運行良好,但在觸發器內部使用時發生錯誤。

這裏的錯誤是,MySQL提供了

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select avg(rating) from ratings join users where ratings.uname=users.uname an' at line 4 

無論是書籍和評級表包含一個稱爲BOOKID場。

請幫助

回答

1

如果你想從一個select語句一個值,該語句是在(括號內)。會發生在new.bookid users

set @n1 = (select avg(rating) from ratings join users where ratings.uname=users.uname 
    and ratings.bookid=new.bookid users.`type`='admin' or users.`type`='critic'); 

下一個錯誤 - 有可能是一個andor失蹤。

+0

謝謝!它的工作! –

+0

所以請將答案標記爲已接受。 –