2

我最近开始研究 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

book 和 rating 表都包含一个名为 bookid 的字段。

请帮忙

4

1 回答 1

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缺失。

于 2012-10-05T20:43:35.483 回答