2013-03-31 50 views
2

我有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事件表。那麼如何從不同的表中獲取不同的值並將它們插入到觸發器中的一個表中呢?

+0

你是什麼意思*無法獲取*?你有錯誤嗎? –

+0

沒有任何錯誤...但是它在'notification'表中的'uid'處插入'0'。 –

回答

1

刪除您declare創建不會@引用的@

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

變量。只有你用set創建的那個。例如:

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

set @id = (select 1); 
select @id; 
相關問題