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.

2
  • Can you add your table definition please. Commented Dec 7, 2018 at 16:34
  • Is table can be used as table name in MySQL? Commented Dec 7, 2018 at 16:35

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Even though this is the correct answer, can you clarify why OP was updating the primary key to itself? What does this change?
Do you really think that this is necessary for such a simple case? Imho it's pretty clear if you compare the statements ;)
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).
I hope you're now satisfied ;)
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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.