I have a table that contains product price and stock by size and color. I put them in JSON format. Table structure like
id | chart
---+--------------------------------------------------------------+
1 | [{"size":"XL", "color":"Red", "stock":"30", "price":"300"}, |
| {"size":"XXL", "color":"Red", "stock":"40", "price":"400"}, |
| {"size":"XXL", "color":"Blue", "stock":"35", "price":"450"}]|
-------------------------------------------------------------------
I want to update the price value to 350, If the size match with XL and the Color with Red. I tried this code,
UPDATE t
SET chart = REGEXP_REPLACE(
JSON_EXTRACT(chart, '$'),
JSON_OBJECT("size", "XL", "color", "Red", "stock", "([0-9]+)", "price", "([0-9]+)"),
JSON_OBJECT("size", "XL", "color", "Red", "stock", "$1", "price", "350")
)
WHERE id = 1;
It executes successfully. But the main problem is stock value changes with $1 string. Here it should be updated with previous stock value which is unknown to me.