0

Mysql 8 makes it possible to extract part of an array saved in a JSON column as shown below:

SELECT jcol->>'$[1 to 5]' FROM `db`.`table` which 1 and 5 are boundaries of the desired range of elements in array.

My question is how to specify the range by numbers from different column. Some thing like this: SELECT jcol->>'$[table2.from to table2.to]' FROM `db`.`table`

asked Jun 2, 2022 at 5:54
1
  • 1
    You must build the JSON path using common string processing. SELECT JSON_UNQUOTE(JSON_EXTRACT(jcol, CONCAT('$[', table2.from, ' to ', table2.to, ']'))) FROM db.table. Commented Jun 2, 2022 at 9:23

1 Answer 1

2

You must build the JSON path using common string processing.

SELECT JSON_UNQUOTE(JSON_EXTRACT(jcol, CONCAT('$[', table2.from, ' to ', table2.to, ']'))) 
FROM db.table

The JSON path is literal and it is not evaluated as an expression, so you must use CONCAT() for the path building.

And the expression usage does not allow to use ->> operator, you must use JSON_UNQUOTE(JSON_EXTRACT()) instead.

answered Jun 2, 2022 at 11:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.