Hardly rocket science, but this annoyed me enough to jot it down
in case it might be useful for others…
The MySQL manual page on INSERT syntax could perhaps be a little more
thorough on how to write INSERTs with ON DUPLICATE KEY UPDATE. So
here’s a slightly more thorough example of valid syntax than the
one given in the manual.
In the example, the reports table’s Primary Key is (id, year)
INSERT INTO reports
SET id='9612', year='2007', net_income=123456, equity=12345,
number_of_employees=123
ON DUPLICATE KEY UPDATE net_income=123456, equity=12345,
number_of_employees=123
Notice in this syntax:
- I’m using SET colname=value syntax in the INSERT part of the
statement. You don’t need to do this, but notice how the parts
marked in blue are the exact same in both parts of the …
[Read more]