I would like to creating a trigger that updates the database when a new message is sent. If the SenderId and RecipentId already have a conversationId, then update the database so that the New.conversationId = existing conversationId.
Create a trigger that updates the conversationId to the same value whenever users 11 & 5 send messages to each other.
delimiter $$
create trigger before_insert_conversationId
before insert on data25k29_mailbox_message
for each row
begin
IF ( EXISTS (
SELECT data25k29_mailbox_message.conversationId FROM data25k29_mailbox_message
WHERE (data25k29_mailbox_message.senderId='5'
and data25k29_mailbox_message.recipientId = '11')
and (data25k29_mailbox_message.senderId= '11'
and data25k29_mailbox_message.recipientId= '5')
)
)
THEN
SET NEW.conversationId = data25k29_mailbox_message.conversationId;
ELSE
SET NEW.conversationId = NEW.conversationId;
END IF;
end
$$
The value doesn't change after inserting values. Any help in this regard is greatly appreciated. Thanks
This is my working snippet of this question.
Drop trigger if exists after_insert_conversationId;
delimiter |
CREATE TRIGGER after_insert_conversationId
before insert ON data25k29_mailbox_message
FOR EACH ROW
BEGIN
Declare v1 int;
select conversationId into v1 from data25k29_mailbox_message
where senderId = new.senderId and recipientId = new.recipientId limit 1;
if v1 is NULL
then
set new.conversationId = new.conversationId;
else
set new.conversationId = v1;
End if;
END|
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.