Merge JSON array values across multiple rows in MySQL
Merge JSON array values across multiple rows in MySQL
1678
27-Jul-2023
Aryan Kumar
07-Aug-2023Here is a SQL query that you can use to merge JSON array values across multiple rows in MySQL:
SQL
This query will first group the rows by the
idandnamecolumns. It will then aggregate thevaluecolumn into a JSON array. Thearray_agg()function will sort the values in the array by thekeycolumn. Thejson_array_agg()function will then create a JSON array from the aggregated values.The following is an example of the output of this query:
As you can see, the query has merged the
valuecolumns from the two rows into a single JSON array. The array is sorted by thekeycolumn.