作为较大的select查询的一部分,我需要从json数组的对象中提取值作为逗号分隔的字符串。
我设法从json对象中获取了json数组:
SELECT * FROM (SELECT json_extract(Site.Login, '$.Uris') FROM Site);
第二个变体给出相同的结果:
SELECT value FROM json_each(Site.Login), Site WHERE json_each.key = 'Uris';
单行给出所需结果的测试:
SELECT group_concat(json_extract(value, '$.Uri')) as login_uri FROM json_each('[{"Uri":"https://cnn.com"},{"Uri":"https://bbc.com"},{"Uri":"https://reuters.com"}]');
我迷失在矩阵中。我已经尝试了多种方法来组合上面的查询代码,但是我没有任何进展。
Site.Login单元的示例。 Uri对象的数量可以从0到无限。
{
"Uris": [
{"Uri":"https://cnn.com"},
{"Uri":"https://bbc.com"},
{"Uri":"https://reuters.com"}
],
"Username": "ghhhhhhhhhhhhhfgggggggggggggggg",
"Password": "hgfhfghfghfgh",
"PasswordRevisionDate": "2019-01-07T21:51:42.65Z",
"Totp": "gffffffffffffffffffffffhhhhhhhhhhhhhhhfghghgfh",
"Name": "hgfhfghfghfghfgh",
"PasswordHistory": [
{
"Password": "ghfghfghfghfghfg",
"LastUsedDate": "2019-01-07T21:51:42.65Z"
}
]
}
“网站”表的完整布局:
CREATE TABLE "Site" (
"Id" varchar primary key not null ,
"FolderId" varchar ,
"UserId" varchar ,
"OrganizationId" varchar ,
"Name" varchar ,
"Notes" varchar ,
"Fields" varchar ,
"PasswordHistory" varchar ,
"Login" varchar ,
"Card" varchar ,
"Identity" varchar ,
"SecureNote" varchar ,
"Favorite" integer ,
"Edit" integer ,
"OrganizationUseTotp" integer ,
"RevisionDateTime" bigint ,
"Type" integer ,
"Data" varchar )
select查询应返回一个名为login_uri的列,其中包含提取的json数组对象值作为连接字符串: https://cnn.com,https://bbc.com,https://reuters.com
答案 0 :(得分:0)
您非常亲密!
SELECT group_concat(json_extract(j.value, '$.Uri')) AS login_uri
FROM site AS s
JOIN json_each(json_extract(s.login, '$.Uris')) AS j
给予
login_uri
---------------------------------------------------
https://cnn.com,https://bbc.com,https://reuters.com
如果您希望表中的每一行都给出结果行,而不是将整个表中的 all uris连接在一起成为一个字符串,请在末尾添加GROUP BY s.id