Question

What I have:

  • A mysql table, say table1
  • table1 contains two columns viz. id and data
  • id is int but the twist is data is JSON 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.

Was it helpful?

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;

fiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top