I’m not optimistic that this can be done without a stored procedure, but I’m curious if the following is possible.
I want to write a single query insert/update that updates a row if it finds a match and if not inserts into the table with the values it would have been updating.
So… something like
updateInsert into table_a set n = 'foo' where p='bar';
in the event that there is no row where p=’bar’ it would automatically insert into table_a set n = ‘foo’;
EDIT:
Based on a couple of comments I see that I need to clarify that n is not a PRIMARY KEY and the table actually needs the freedom to have duplicate rows. I just have a situation where a specific entry needs to be unique… perhaps I’m just mixing metaphors in a bad way and should pull this out into a separate table where this key is unique.
I would enforce this with the table schema – utilize a unique multi-column key on the target table and use
INSERT IGNORE INTO– it should throw an error on a duplicate key, but the insert will ignore on error.