1

I have an INSERT INTO ... ON DUPLICATE KEY UPDATE query that for some reason is being ignored by the mysql server, at least as far as I can tell.

It's catching on an update of an existing row.

I have a primary key of "id" and part_no is listed as Unique.

Here is the query:

INSERT INTO parts_prices (cost, 
chatsworth, 
atlanta,
north_carolina,
new_jersey,
arizona, 
colorado,
miami,
indianapolis, 
ohio, 
oregon,
dallas,
san_antonio,
washington,
memphis,
alt_sup,
part_no)
VALUES (0.00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '', 'HS3163')
ON DUPLICATE KEY UPDATE 
cost=VALUES(cost), chatsworth=VALUES(chatsworth), atlanta=VALUES(atlanta), north_carolina=VALUES(north_carolina), new_jersey=VALUES(new_jersey), arizona=VALUES(arizona), colorado=VALUES(colorado), miami=VALUES(miami),
indianapolis=VALUES(indianapolis), ohio=VALUES(ohio), oregon=VALUES(oregon), dallas=VALUES(dallas), san_antonio=VALUES(san_antonio), washington=VALUES(washington), memphis=VALUES(memphis), alt_sup=VALUES(alt_sup)

The first column is a decimal, then a bunch of ints, and the last two are varchars. This is an entry in a parts list database (we happen to list the part number, but don't actually have it yet). I'm iterating through a list of all of the part numbers (by using a last_modified column) and this particular query doesn't update the timestamp.

Could this be because the information in the row isn't actually changing (all specified values are identical to the initial values)?

The query worked for (quite a few) previous entries in the database.

[edit] I've checked for response errors in the query from the PHP script and directly (using phpmyadmin), they do not return any error string. [/edit]

1 Answer 1

2

on update current_timestamp only takes effect if at least one of the columns is actually updated. If you try to update a row with the values it already has, it doesn't count as an update (the affected rows count is unchanged) and the on update doesn't take effect.

It would be more reliable to manually add last_modified=now() to your update.

1
  • Thanks! worked like a charm! I'll accept your answer as soon as it lets me.
    – Riet
    Oct 16, 2012 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.