用于React Native、
假设我有一个SQLite数据库表,其中包含col1主col2。
其中col1包含序列号,col2包含类似JSON的
col1 : col2
1 : {"id":"id1", "value":"value1"},
2 : {"id":"id2", "value":"value2, value3"},
3 : {"id":"id3", "value":"value4, value5"}
我只想使用SQLite查询来提取那些唯一的值,我期望的输出是:["value1","value2","value3","value4","value"]
您可以使用json_extract()
:
select group_concat(json_extract(col2, '$.value'), ', ') result
from tablename
结果:
> result
> -------------------------------------
> value1, value2, value3, value4, value5
或者,如果您希望将结果格式化为json数组,请使用json_group_array()
:
select replace(json_group_array(json_extract(col2, '$.value')), ', ', '","') result
from tablename
结果:
> | result |
> | :--------------------------------------------- |
> | ["value1","value2","value3","value4","value5"] |
如果你可以使用JSON1扩展,那么你可以使用字符串函数:
select group_concat(substr(
col2,
instr(col2, '"value":"') + length('"value":"'),
length(col2) - (instr(col2, '"value":"') + length('"value":"') + 1)
), ', ') result
from tablename
结果:
> result
> -------------------------------------
> value1, value2, value3, value4, value5
请参阅演示