0

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.

The50
  • 1,096
  • 2
  • 23
  • 47

1 Answers1

2
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.

maio290
  • 6,440
  • 1
  • 21
  • 38
  • Even though this is the correct answer, can you clarify why OP was updating the primary key to itself? What does this change? – Blue Dec 07 '18 at 16:48
  • Do you really think that this is necessary for such a simple case? Imho it's pretty clear if you compare the statements ;) – maio290 Dec 07 '18 at 16:53
  • It is if you want to make a truly complete answer, and have my upvote. I personally would have explained to OP, that the update statement is not what row that you're trying to update, but it's the actual columns that need updating (So in this case, he should be attempting to update the score_count field instead). – Blue Dec 07 '18 at 16:55
  • I hope you're now satisfied ;) – maio290 Dec 07 '18 at 16:57
  • I am: This is now a much more complete answer, and explains the reason *why* OP was having issues. These are the types of answers that are more useful to future readers who happen upon this question in the future. It may take a bit more effort, but in the end, it's quality questions and answers that drive users to this site. – Blue Dec 07 '18 at 16:59