Possible Duplicate:
How do I update if exists, insert if not (aka upsert or merge) in MySQL?
I have a table with the columns user, category and score. I want to run an insert that, if this user/category combination already exists instead just updates the score. Both the user and the category column can have the same value many times but each specific user/category combination may only exist on one single row. How can I do this?
Many thanks
For this example, let’s call your table scores:
Make sure you have a unique index
Now run this to replace:
If your updating the score means add up scores, then do this:
Give it a try!
UPDATE 2011-08-28 09:00 EDT
If you want to keep just high scores and then add the unique index, here is what you must do:
UPDATE 2011-08-28 10:53 EDT
Run this to keep maximum score: