gpt4 book ai didi

mysql - 如何从不同的表中获取值并将其传递给触发器?

转载 作者:搜寻专家 更新时间:2023-10-30 20:41:59 24 4
gpt4 key购买 nike

我有 3 个表:

event:
---------------------
event_id | uid | date
---------------------

event_invitees
----------------------
invitees_id | event_id
----------------------

notification
-----------------
uid | invitees_id
-----------------

我已经创建了一个触发器,它将在插入“event_invitees”后插入到“通知”表中

delimiter |
create trigger eventNotificationTrigger after insert on event_invitees
for each row
begin
declare id int(12);
set id = (select uid from event where event_id=new.event_id);
insert into notification (invitees_id,uid) select invitees_id,@id from event_invitees where event_id=new.event_id;
end;
|

但它无法从事件表中获取 uid。那么如何从不同的表中获取不同的值并在触发器中将它们插入到一个表中呢?

最佳答案

删除@

insert into notification (invitees_id,uid) 
select invitees_id, @id from event_invitees where ...
^-------------------------------------here

您使用 declare 创建的变量不会被 @ 引用。只有您使用 set 创建的。示例:

declare id int(12);
select 1 into id;
select id;

set @id = (select 1);
select @id;

关于mysql - 如何从不同的表中获取值并将其传递给触发器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15728480/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com