Similar to this question, I want to INSERT
a row into a MySQL table if the row, identified by the value of only one column, doesn't exist, and UPDATE
it if it does. However, I want to simply add one to an integer column of the row instead of changing the column value entirely.
The table is as follows:
ref VARCHAR(16), num INT
If "value" exists as ref
in the table, I want to:
UPDATE table SET num = num + 1 WHERE ref = "value";
But if it doesn't exist, I want to:
INSERT INTO table SET ref = "value", num = 1;
I know that there are a large number of questions about inserting if non-existent, update if existent, many with a good answer, but none seem to answer this problem directly. I'm sure it's simple, but I worry that the ON DUPLICATE KEY
would require both columns to be identical. In my case, it needs to work, no matter what number num
is.