Postgresql 查找具有特定数组值的postgres jsonb列的所有行
(使用Postgres 9.5) 假设您有一个具有以下架构的Postgres表:Postgresql 查找具有特定数组值的postgres jsonb列的所有行,postgresql,jsonb,postgresql-9.5,Postgresql,Jsonb,Postgresql 9.5,(使用Postgres 9.5) 假设您有一个具有以下架构的Postgres表: Table "public.tableA" Column | Type | Collation | Nullable | Default ---------------+---------------
Table "public.tableA"
Column | Type | Collation | Nullable | Default
---------------+--------------------------+-----------+----------+----------------------------------------------
id | bigint | | not null | nextval('truelayer_tokens_id_seq'::regclass)
user_id | text | | not null |
created_at | timestamp with time zone | | | now()
updated_at | timestamp with time zone | | |
provider_id | text | | not null | ''::text
scopes | jsonb | | not null | '{}'::jsonb
在scopes列中是以下各种用户ID数据的子集。
[“A”、“B”、“C”、“D”、“E”]
- 某些用户将具有作用域:
[“A”]
- 一些用户将具有作用域:
[“B”、“C”、“D”]
- 一些用户将具有作用域:
[“A”,“D”]
我甚至很难开始这样做,所以所有的建议都很受欢迎。使用
jsonb
containment操作符@
:
SELECT id, scopes
FROM "tableA"
WHERE scopes @> '["A"]';