问题
My query:
INSERT INTO `table` (`article_id`, `score_count`) VALUES (1922, '{\"1\":3,\"2\":2,\"3\":10,\"4\":2,\"5\":1}') ON DUPLICATE KEY UPDATE `article_id`= 1922
And my article_id column is set as primary unique key. After I run this I get 0 rows inserted and no update.
回答1:
INSERT INTO `table` (`article_id`, `score_count`)
VALUES (1922, '{\"1\":3,\"2\":2,\"3\":10,\"4\":2,\"5\":1}')
ON DUPLICATE KEY
UPDATE `score_count`= '{\"1\":3,\"2\":2,\"3\":10,\"4\":2,\"5\":1}'
Since you don't want to update the primary key to itself.
ON DUPLICATE KEY UPDATE
updates the specified column to a value, if a duplicate key was found. You were updating article_id
which was already 1922
to 1922
. See the offical reference.
来源:https://stackoverflow.com/questions/53673350/php-mysql-insert-on-duplicate-key-update-not-working