有一个'帐户'表格中包含' :: 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"}'
但它没有显示记录。什么错过了?
答案 0 :(得分:1)
您的查询需要以下形式的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 @>适用于两个对象或两个数组。