1

我在 phpmyadmin 中创建了以下触发器。

delimiter |
create trigger active after insert on event
for each row
begin
declare n int;
set n = (select follower_id from follow where circle_id=new.circle_id);
insert into activity (uid, performed_activity_id,activity_type_id) values(new.uid,new.event_id,'1');
insert into notification (sender_id,receiver_id,message)values (new.uid,n,new.title);
end;
|

我在这里遇到的问题是 select 语句返回了更多的一个值。

ERROR 1242 (21000): Subquery returns more than 1 row

如何从 select 语句中获取多个值以及如何将它们传递给 insert statemant。?

4

1 回答 1

2

问题出在这条线上,

set n = (select follower_id from follow where circle_id=new.circle_id);

变量只能保存一个值。如果查询结果为或者一个值就可以了,否则会抛出select查询返回多个值的错误信息。

但可以通过添加来解决LIMIT,例如

set n = (select follower_id from follow where circle_id=new.circle_id LIMIT 1);

更新 1

delimiter |
create trigger active after insert on event
for each row
begin

    insert into activity (uid, performed_activity_id, activity_type_id) 
    values (new.uid, new.event_id, '1');

    insert into notification (sender_id, receiver_id, message)
    select new.uid, follower_id, new.title
    from   follow 
    where  circle_id=new.circle_id;

end |
delimiter ;
于 2013-03-02T11:08:37.300 回答