Link to home
Start Free TrialLog in
Avatar of Dusty
DustyFlag for United States of America

asked on

MySQL Update Trigger question

I want to track latest Price change for an item.
The table name is res, field names are price, prevprice, priceupdatedate

I need help with writing a trigger that updates the field "prevprice" with the old price when the "price" field is updated and then writes the current time stamp to the priceupdatedate field.

code example appreciated! Thanks!
Avatar of alpmoon
alpmoon
Flag of Australia image

Actaully there are useful sample trigger code on the web:

http://dev.mysql.com/doc/refman/5.5/en/trigger-syntax.html
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dusty

ASKER

@ChrisStanyon
Thanks for the help!

Running the trigger did work for the setting the NEW.prevprice = OLD.price

The priceupdateddate column I changed to timestamp but it did not update the field when updating the price.

I tried:

SET NEW.prevprice = OLD.price, priceupdatedate = NOW();

but that didn't work either says my version of MySQL doesnt support multiple triggers with same action time and event for one table.
if you edit the trigger using the code I provided it will try and create the same trigger again so you need to delete it first. the timestamp column should update automatically but only if it's the only timestamp column in your table. that may depend on your version of mysql
Avatar of Dusty

ASKER

Works perfect!
Thanks so much!