[英]PostgreSQL 9.6 jsonb query using like on arrays
i need to query a jsonb table field with the normal like functions.我需要用普通的类似函数查询一个 jsonb 表字段。
This is my json field这是我的 json 字段
"campi":[
{
"label":"testLabel",
"valore":[
"testValore",
"testValore2"
],
"idCampo":"testID",
"idCampoTP":"testCampoID",
"proprieta":[
{
"label":"testLabel",
"idProprieta":"testProp"
}
],
"idTipoCampo":"idTipoCampoID"
},
{
"label":"testLabel2",
"valore":[
"testValore3",
"testValore4"
],
"idCampo":"testID2",
"idCampoTP":"testCampoID2",
"proprieta":[
{
"label":"testLabel2",
"idProprieta":"testProp2"
}
],
"idTipoCampo":"idTipoCampoID2"
}
]
}
Is even possibile make a query like this?甚至可以进行这样的查询吗?
SELECT customfield from procedura WHERE customfield->'campi' @> '[{"label":"testLabel3"}]'
But with testLabel3 with like wildcards: testLabel%但是使用带有类似通配符的 testLabel3:testLabel%
Another question, is even possibile make a query for get the object(s) "campi" with a "valore" of "testValore"?另一个问题,是否甚至可以查询以“testValore”的“valore”获取对象“campi”?
My dream query was:我的梦想查询是:
SELECT customfield from procedura WHERE customfield->'campi' @> '[{"label":"testLabel%"}]'
With % as wildcard以 % 作为通配符
EDIT:编辑:
I faund a way to make some simple query:我找到了一种方法来进行一些简单的查询:
SELECT customfield FROM procedura, jsonb_array_elements(procedura.customfield #> '{campi}') obj
WHERE obj->>'idCampoTP' LIKE 'testCampoID3%' group by procedura.id;
but i cant figure how to search in valore field sub-array但我不知道如何在 valore 字段子数组中搜索
EDIT:编辑:
I found this way, but to me seem a crap solution我找到了这种方式,但对我来说似乎是一个垃圾解决方案
SELECT customfield FROM procedura, jsonb_array_elements(procedura.customfield #> '{campi}') obj
WHERE obj->>'valore' LIKE '%stValore5%' group by procedura.id;
Yes it works:)是的,它有效:)
For filtering of type 'testValore' as you have already mentioned in you question用于过滤您在问题中已经提到的“testValore”类型
data->'campi' @> '[{"label":"testLabel3"}]';数据->'campi'@>'[{"label":"testLabel3"}]';
For extracting id with valore of type 'testValore'用于提取类型为“testValore”的 id
data->'campi' @> '[{"valore": ["testValore"]}]';数据->'campi'@>'[{"valore": ["testValore"]}]';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.