我需要找到PostgreSQL数据库(9.5)中的所有记录,其中connections
JSONB列包含包含一些信息的哈希数组。此搜索查询必须不区分大小写。
例如,列为[{"type":"email", "value":"john@test.com", "comment": "Test"}, {"type":"skype", "value":"john.b", "comment": "Test2"}]
)。我需要找到记录,其中connections
列包含类型为“skype”的条目,值为“JOHN.B”。
# SELECT * FROM contacts;
id | email | connections
---+-------+------------------------------------------------------------------------------
1 | asd | [{"type": "email", "value": "john@test.com"}, {"type": "skype", "value": "john.b"}]
我该怎么办?感谢。
答案 0 :(得分:1)
WITH t(id,email,connections) AS ( VALUES
(1,'test','[
{"type": "email", "value": "john@test.com"},
{"type": "skype", "value": "john.b"}
]'::JSONB)
)
SELECT * FROM t
WHERE connections @> '[{"type": "skype"}]'
AND connections @> '[{"value": "john.b"}]';
结果:
id | email | connections
----+-------+-------------------------------------------------------------------------------------
1 | test | [{"type": "email", "value": "john@test.com"}, {"type": "skype", "value": "john.b"}]
(1 row)
答案 1 :(得分:0)
以下是使用>@
的一种方法:
where connections @> '{"type":"skype"}' and
connections @> '{"value":"john.b"}')