Skip to content
Advertisement

Update column values with dynamically created JSON MySql

I have a Relationships table that looks something like this

And 2 tables From and To

I’m trying to update the JsonPair column with Json objects in the form {FromName: ToName}. So the resulting table would look like

I’m a novice at SQL. I’m thinking that I should SELECT the names first, then use the result to put in the UPDATE statement.

So far I got this to return FromName and ToName

and

Then I think I should use the result of this to do

I’m stuck at the combining and format step. Can I get help with this please?

I’m using MySql

Advertisement

Answer

You can use the update/join syntax, along with json_object().

Consider:

Note: from and to are reserved words in MySQL, hence bad choices for table names. I renamed them to t_from and t_to in the query.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement