例如,我想计算一个字符串中单词出现的次数,
[{"lastUpdatedDateTime":{"timestamp":1.54867752522E12},"messageStatus":"DELIVERED","phoneNumber":"+916000060000"},{"lastUpdatedDateTime":{"timestamp":1548677525220},"messageStatus":"DELIVERED","phoneNumber":"+916000060000"}]
提前谢谢。
答案 0 :(得分:2)
如果表名是tablea
,而列名是col
:
SELECT
(LENGTH(col) - LENGTH(REPLACE(col, '"DELIVERED"', '')))
/
LENGTH('"DELIVERED"') as counter
from tablea
删除每次出现的"DELIVERED"
,然后从原始字符串中减去字符串的长度,最后将结果除以"DELIVERED"
的长度
答案 1 :(得分:1)
假设您的数据在表中,例如:
CREATE TABLE example(json TEXT);
INSERT INTO example VALUES('[{"lastUpdatedDateTime":{"timestamp":1.54867752522E12},"messageStatus":"DELIVERED","phoneNumber":"+916000060000"},{"lastUpdatedDateTime":{"timestamp":1548677525220},"messageStatus":"DELIVERED","phoneNumber":"+916000060000"}]');
并且您的sqlite实例启用了JSON1扩展名:
SELECT count(*) AS "Number Delivered"
FROM example AS e
JOIN json_each(e.json) AS j
WHERE json_extract(j.value, '$.messageStatus') = 'DELIVERED';
给您
Number Delivered
----------------
2
这将返回表中所有行中匹配条目的总数作为单个值。如果您希望每行一个结果,这是一个简单的更改,但具体细节取决于您的表定义。不过,在大多数情况下,在查询末尾添加GROUP BY e.rowid
即可。
从长远来看,将数组中的每个对象存储为表中的一行,并分解为适当的列可能是一个更好的主意。