Searching inside JSON array in MySQL

2019-09-16 06:41发布

问题:

I have a table of Products that also contains JSON datatype with JSON array - json_array() field.

For the example "Products" table includes the following:

ProductID
ProductName
ProductDesc
CreateDate
Price
HistoricalPrices (json) 

I am appending the data using the follow query syntax:

    UPDATE Products 
SET    HistoricalPrices =
           json_array_append(HistoricalPrices, 
               '$', json_object('CreateDate', '2016-05-01', 'Price', 23.65)
           )
WHERE  ProductID = 1;

Example of structure of the JSON array:

> [
>     {
>         "CreateDate": "2016-05-01",
>         "Price": 12.34
>     },
>     {
>         "CreateDate": "2016-05-22",
>         "Price": 12.50
>     } ]

Would it be possible to search inside the JSON array for a specific productID. Like looking for specific prices in a date range ?