PHP MySQL INSERT ON DUPLICATE KEY UPDATE not working

僤鯓⒐⒋嵵緔 提交于 2019-12-13 03:34:48

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!