PostgreSQL:在JSONB列中按json属性搜索,该列填充为json数组
所以我有这个:PostgreSQL:在JSONB列中按json属性搜索,该列填充为json数组,sql,postgresql,jsonb,Sql,Postgresql,Jsonb,所以我有这个: CREATE EXTENSION "uuid-ossp"; -- not related, just for the uuid_generate_v4 CREATE TABLE events ( id UUID PRIMARY KEY NOT NULL DEFAULT uuid_generate_v4(), speakers JSONB NOT NULL DEFAULT '[]'::jsonb ); INSERT INTO events (id, speakers)
CREATE EXTENSION "uuid-ossp"; -- not related, just for the uuid_generate_v4
CREATE TABLE events (
id UUID PRIMARY KEY NOT NULL DEFAULT uuid_generate_v4(),
speakers JSONB NOT NULL DEFAULT '[]'::jsonb
);
INSERT INTO events (id, speakers) values (uuid_generate_v4(), '[
{
"id": "de3ae2c7-19f6-4c69-81ae-467034c06101",
"email": ""
},
{
"id": "c8cf8fe8-a6b7-4cbc-b2c7-729c6108ff5f",
"email": "hello@example.com"
}
]'::JSONB)
我需要获取event.id的列表hello@example.com'出现在发言人JSONB列的“电子邮件”字段中至少一次
我试过:
select id from events where events.speakers->'email' ? '"hello@example.com"';
<no results>
从事件中选择id,其中events.speakers->“email”?'"hello@example.com"';
还有许多其他从未奏效的片段。我不认为这是一个如此不同寻常的使用模式 首先必须使用jsonb\u数组元素展开数组
SELECT * FROM (
select id, jsonb_array_elements(speakers) as event from events
) as a where event->'email' ? 'hello@example.com'