我最近开始使用 MySQL,现在我想创建一个触发器,但 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
books 和 ratings 表都包含一个名为 bookid 的字段。
请帮忙
最佳答案
如果您想要来自 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
- 可能缺少 and
或 or
。
关于触发器内的 select 语句中的 MySQL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12753705/