Postgres 9.4 select by json 数组的元素值
Postgres 9.4 select by json array`s element value
有一个 'accounts' table 'data'::jsonb 字段填充:
{
"cars": [{"body": "E-JZA80-ALFQZ",
"year": 1999,
"brand": "Toyota",
"model": "Vista Ardeo"}
],
"name": "Gilbert Moore",
"phone": "+13222314555"
}
我正在尝试:
select * from accounts where data->'cars' @> '{"brand":"Toyota"}'
但它不显示记录。错过了什么?
您的查询需要 json 格式的值:
{
"cars": {"body": "E-JZA80-ALFQZ",
"year": 1999,
"brand": "Toyota",
"model": "Vista Ardeo"}
,
"name": "Gilbert Moore",
"phone": "+13222314555"
}
但是在实际数据中data->'cars'
是一个数组,不是对象,所以查询应该是:
select a.*
from accounts a
where data->'cars' @> '[{"brand":"Toyota"}]'
as operator @> 适用于两个对象或两个数组。
有一个 'accounts' table 'data'::jsonb 字段填充:
{
"cars": [{"body": "E-JZA80-ALFQZ",
"year": 1999,
"brand": "Toyota",
"model": "Vista Ardeo"}
],
"name": "Gilbert Moore",
"phone": "+13222314555"
}
我正在尝试:
select * from accounts where data->'cars' @> '{"brand":"Toyota"}'
但它不显示记录。错过了什么?
您的查询需要 json 格式的值:
{
"cars": {"body": "E-JZA80-ALFQZ",
"year": 1999,
"brand": "Toyota",
"model": "Vista Ardeo"}
,
"name": "Gilbert Moore",
"phone": "+13222314555"
}
但是在实际数据中data->'cars'
是一个数组,不是对象,所以查询应该是:
select a.*
from accounts a
where data->'cars' @> '[{"brand":"Toyota"}]'
as operator @> 适用于两个对象或两个数组。