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.
Deepam Gupta
-
1for starters which mysql version are you using? basically json is bad for such problems, mysql 8 has json_table which simplifies it somewhat, still json is bad if you want that information directly from mysql, store tem in a normal relational table and it gets easynbk– nbk2021年03月10日 14:51:02 +00:00Commented Mar 10, 2021 at 14:51
-
I'm using MariaDB 10.4Deepam Gupta– Deepam Gupta2021年03月10日 15:33:38 +00:00Commented Mar 10, 2021 at 15:33
-
Synthetic numbers list and JOIN.Akina– Akina2021年03月10日 15:50:05 +00:00Commented Mar 10, 2021 at 15:50
-
@Akina the oath must be strings or not`?nbk– nbk2021年03月10日 16:19:34 +00:00Commented Mar 10, 2021 at 16:19
-
@nbk Generated numbers will be used for to build JSON paths - so number during generation and string finally.Akina– Akina2021年03月10日 16:27:14 +00:00Commented Mar 10, 2021 at 16:27
1 Answer 1
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;
answered Mar 10, 2021 at 16:44
-
Thanks @Akina, learned something new today.Deepam Gupta– Deepam Gupta2021年03月11日 05:20:38 +00:00Commented Mar 11, 2021 at 5:20
lang-sql