如何检索触发器中select语句返回的多个值?

时间:2013-03-02 11:07:32

标签: mysql sql triggers subquery

我在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语句中获取多个值以及如何将它们传递给插入statemant。?

1 个答案:

答案 0 :(得分:2)

问题出在这一行,

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

变量只能包含一个值。如果查询结果是 null 或一个值,那么它没问题,否则会抛出一条错误消息,表明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 ;