A
A
Andrey Lepaskaln2020-05-03 14:23:26
MySQL
Andrey Lepaskaln, 2020-05-03 14:23:26

How to find the maximum and minimum value in a JSON array using sql query in MySQL?

The task is this, a database in MySQL, the table has 10 rows, one of the columns is of the JSON type.
I need to find the maximum and minimum value in a JSON column using sql query.
Table example:
id JSON_Col
1 {"Strength": [0]}
2 {"Strength": [12, 20, 35]}
3 {"Strength": [3]}
4 {"Strength": [3]}
5 {"Strength": [3]}
6 {"Strength": [0,23,58]}
7 {"Strength": [3]}
8 {"Strength": [3]}
9 {"Strength": [ 3]}
10 {"Strength": [3]}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2020-05-04
@holoron

select max(Strength) max_St, min(Strength) min_St
from table
, JSON_TABLE(concat('[',JSON_Col,']'),
    '$[*]'
    COLUMNS(
    NESTED PATH '$.Strength[*]' COLUMNS (Strength INT PATH '$') )
   ) as tt

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question