Skip to content
Advertisement

How to Group By some id and Sum all row json encode data

Using this, PHP 7.2, Mysql 5 and Apache 2.

MY table data ,

it is my table

My expectation result,

expected result

Advertisement

Answer

In MySQL version 5.7, JSON functions were introduced that could solve your problem. You could use the following, but it only allows to retrieve the sums when a fixed number of items is included in your JSON. Your example points out that there are 3 items in every JSON array, so you should be fine:

If you would upgrade to MySQL 8, you could also use the new JSON_TABLE function to process any number of items in the JSON array:

You can test this out in this db fiddle as well.

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