Mysql has a special 'INSERT ... ON DUPLICATE KEY UPDATE' statement which can be used for some models like e.g. ratings. Instead of something similar to this:
You can just do this:
You use it like this:
Warning: You need mysql 5.1 so that LAST_INSERT_ID(id) works. (Mysql bug)
You can just do this:
when the row already exists in the database (detected by a unique-constraint violation) the values of that row are updated and saved, otherwise the new row is created.
My current solution is this:You use it like this:
Warning: You need mysql 5.1 so that LAST_INSERT_ID(id) works. (Mysql bug)





Leave a comment