在触发器内的select语句中出现MySQL错误

时间:2012-10-05 20:39:29

标签: mysql select triggers

我最近开始研究MySQL n我现在想创建一个触发器,但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 个答案:

答案 0 :(得分:1)

如果您想要select语句中的单个值,则该语句必须位于(括号)中。

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');

下一个错误将发生在new.bookid users - 可能会丢失andor