I have a table that looks similar to this…
+-------------+-------------+
| Field | Type |
+-------------+-------------+
| var1 | varchar(15) |
| var2 | varchar(25) |
| var3 | int(1) |
+-------------+-------------+
The issue is that I need to be able to insert the 3 variables, but only if var1 and var2 aren’t already in the table. If var1 and var2 are already in the table, var3 should be updated.
So for example
INSERT INTO table (var1, var2, var3) VALUES('0','1','2')
Would insert the 3 values, but
INSERT INTO table (var1, var2, var3) VALUES('0','1','1')
Would update (‘0′,’1′,’2’) to be (‘0′,’1′,’1’)
The important thing is that if var1 and var2 exist, a duplicate entry with a different var3 is not entered.
I’ve looked around at a few other questions, but most of them seem to recommend IGNORE which I don’t think will work for me since I’m checking for multiple duplicates.
First, set
(var1,var2)as combinedPRIMARY KEYThen use
INSERT ON DUPLICATE UPDATE