I’m writing a trigger that will do a cross table update, but I can’t get the desired result. I’m working with one param, and that is messageId. Using that messageId, I need to go into the messages table and find the userId @messageId, then update his/her score: (@messageId is coming from an insert into a third table, so really it’s NEW.messageId)
UPDATE users
SET users.score = users.score + 1
WHERE users.id = messages.userId
I need to select messages.userId @messageId from messages, but the trigger will not let me do a select statement, so I’m a bit stuck!
I tried this, which does not seem to work either:
UPDATE users, messages
INNER JOIN messages ON messages.id = NEW.messageId
SET users.score = users.score + 1
WHERE users.id = messages.userId
PS: I’m not really looking to select anything, I just want to update the users table by referencing it though the messages table, which is in turn being referenced by NEW.messageId
I do not know of there being a way to insert a select statement into a trigger. However, you can use the following syntax if you are looking to monitor for changes in a user’s score: