0

I have a params column in my MySQL database containing this: '{"usergroup" : "1", "language" : "ENG"}'; . Can I change only the language value ENG to GER in just 1 step using a standard PDO update?

Obviously, I could just SELECT the whole content of my table cell '{"usergroup" : "1", "language" : "ENG"}'; , decode it with json_decode, change ENG to GER and UPDATE the table cell in a second step.

I'm looking for a shortcut that allows me to UPDATE only ENG to GER without touching the other data or decode/encode the whole data set.

4
  • 1
    Why do you have a single column in JSON instead of columns? Commented Aug 17, 2021 at 10:38
  • I'm going to add further data to this JSON data set later, so I though it's a more flexible solution than adding more and more columns later on. Commented Aug 17, 2021 at 10:53
  • 2
    Take a read that json column vs multiple columns Commented Aug 17, 2021 at 10:55
  • Interesting read, thank you very much. For my current small project, it doesn't really matter. But for a bigger project, I'm going to use multiple columns instead. Commented Aug 17, 2021 at 12:02

1 Answer 1

2

You can use the JSON_REPLACE function for this.

UPDATE `MyTable`
SET `json` = JSON_REPLACE(json, '$.language', 'GER')
WHERE ...

Here is an overview of other JSON manipulation functions: https://dev.mysql.com/doc/refman/5.7/en/json-function-reference.html.

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

Comments

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.