I'm relatively new to triggers in MYSQL, so sorry if this is a pretty basic thing I'm trying to do. I've found how to set one up allowing a static update, however I haven't seen anything about how to use one of the fields from the inital update as a variable inside the trigger statement
Example:
Table 1, items:
id | name | total_stock
1 | item | 8
2 | item2 | 0
Table 2, item_options:
id | item_id | option | stock
1 | 1 | test | 5
2 | 1 | test2 | 3
3 | 2 | test | 0
If I then update item_options:
UPDATE `item_options` SET `stock`=7 WHERE `id`=1
Or insert a new item into item_options:
INSERT INTO `item_options` (`item_id`,`option`,`stock`) VALUES ('2','add','2')
Then I'd like (if it's possible) to use a trigger to update the total_stock in the items table with the SUM of stock in the item_options table with the same corresponding item_id.
So, I guess my question is in two parts:
- Is this possible?
- Can someone point me in the right direction of how to do this?