2012-10-05 17 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

所以请将答案标记为已接受。 –