使用:Postgres 14.2。
目标:获取表中所有不同国家/地区的列表。
该列address
是一种JSONB
列类型,包含一个结构如下的数组:
{
"address":[
{
"types":["route"],
"long_name":"20203 113B Ave",
"short_name":"20203 113B Ave"
},
{
"types":["locality","political"],
"long_name":"Maple Ridge",
"short_name":"Maple Ridge"
},
{
"types":["administrative_area_level_3","political"],
"long_name":"Maple Ridge",
"short_name":"Maple Ridge"
},
{
"types":["administrative_area_level_2","political"],
"long_name":"Greater Vancouver",
"short_name":"Greater Vancouver"
},
{
"types":["administrative_area_level_1","political"],
"long_name":"British Columbia",
"short_name":"BC"
},
{
"types":["country","political"],
"long_name":"Canada",
"short_name":"CA"
},
{
"types":["postal_code"],
"long_name":"V2X 0Z1",
"short_name":"V2X 0Z1"
}
]
}
Run Code Online (Sandbox Code Playgroud)
如何过滤这个对象数组,使其仅返回数组索引(如果包含)的值"long_name"
(例如) ?Canada
types
"country"
我正在尝试类似的事情,但显然,我只想返回国家/地区,而不是整个品牌。
SELECT * from brand
where address::text ilike ANY (ARRAY['%country%'::text]);
Run Code Online (Sandbox Code Playgroud)
此查询失败并显示:
Run Code Online (Sandbox Code Playgroud){ "address":[ …