JSON in MySQL : get data from an object within a range of keys
-
17-03-2021 - |
Question
What I have:
- A
mysql
table, saytable1
table1
contains two columns viz.id
anddata
id
isint
but the twist isdata
isJSON
type and its keys are comparable- the
table1
contains only one row (for the sake of this question)
table1
id | data |
---|---|
1 | {'1': 'content1', '2': 'content2', '3':'content3',.......,'10000':'content10000' } |
What I want to have:
I want a query such that it returns me key-value pairs within a range of keys, say, 100 to 200.
What I'm getting on searching on internet:
Everywhere I got only the answers where one can get rows which have the values within a range, but here the case is I want values of keys within a range.
Solution
Possible realization:
WITH RECURSIVE cte AS ( SELECT @from num
UNION ALL
SELECT num+1 FROM cte WHERE num < @till )
SELECT CONCAT('key', num) `key`,
JSON_EXTRACT(test.val, CONCAT('$.key', num)) `value`
FROM test
CROSS JOIN cte
HAVING `value` IS NOT NULL;
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange