在 jsonb 中选择数组内的值

Selecting values inside array in jsonb

我有以下table

CREATE TABLE country (
    id      INTEGER NOT NULL PRIMARY KEY ,
    name        VARCHAR(50),
    extra_info  JSONB
);
 
INSERT INTO country(id,extra_info)
VALUES (1, '{ "name" : "France", "population" : "65000000", "flag_colours": ["red", "blue","white"]}');
 
INSERT INTO country(id,extra_info)
VALUES (2, '{ "name": "Spain", "population" : "47000000", "borders": ["Portugal", "France"] }');

SELECT extra_info->>'name' as Name, extra_info->>'population' as Population
FROM country

我想select id 和额外信息

SELECT id,extra_info->>'population' as Population,extra_info->'flag_colours'->>1 as colors
FROM country 

此查询仅显示 id,population 但 flag_colors 为空。

我也想在条件

中使用flag_colors
SELECT extra_info->>'population' as Population FROM country where extra_info->'flag_colours'->>0 

我收到这个错误

ERROR:  argument of WHERE must be type boolean, not type text
LINE 1: ...o->>'population' as Population FROM country where extra_info...
                                                             ^
SQL state: 42804
Character: 67

如何解决这两个问题?

我的查询是这样写的

SELECT *
FROM country
WHERE (extra_info -> 'flag_colours') ? 'red' and (extra_info -> 'flag_colours') ? 'white'

非常感谢alt-f4

更新答案