Here is something that I recently found very useful. If you have a table in your MySQL database which you want to insert a record if it does not exist but update the row if it does. MySQL 5.x has a very neat piece of script to handle this with out any conditional statements.
By using the ON DUPLICATE KEY after the insert statement you don’t need to specify which row needs updating, only the columns because it works it out on the unique key column. The unique key column is essential to make the update work else it will just insert all the time. See MySQL development area to read more about it
It is important to note: that you need MySQL 5 or greater for this script to work. Otherwise you will have to use the old school method.
First test the update statement then if that returns zero affected rows then you run the insert statement. Simples. But of course this is much more code and less efficient. Upgrade if you can!!