I want to update a record which may or may not be present in a table. If it is not present in the database then it will be inserted.
To prevent from select I am using UPDATE statement first and checking affected_rows > 0 if not then I am inserting this record into the table.
I was wondering if there is a better way to do this?
You could use
INSERT ... ON DUPLICATE KEY UPDATEsyntax:http://dev.mysql.com/doc/refman/4.1/en/insert-on-duplicate.html
The difference between this and
REPLACE(Femaref’s answer) is thatREPLACEwill delete the old row and then insert a new row if a key is duplicated, while this will update the existing row.