PostgreSQL jsonb 跨多行的嵌套模式匹配文本搜索
PostgreSQL jsonb nested pattern matching text search across multiple rows
如标题所述,我正在尝试执行一个查询,该查询对多行进行全文搜索,再次是 jsonb 数据类型,带有嵌套数据,问题如下:
CREATE TABLE books (id int primary key, title text, info jsonb);
INSERT INTO users (id, t, j) VALUES
(1, 'title 1', '{"Characters": [{"Name": "foo"}]}'),
(2, 'title 2', '{"Characters": [{"Name": "foo"},{"Name": "bar"}]}');
(3, 'title 3', '{"Characters": null}');
问题:
我想通过每本书中人物的名字查询书籍。例如查询每本书都有一个名为 "foo" 的字符。虽然上面的大纲只是一个例子,但我的现实生活场景要求我使用“~*”运算符搜索角色的名字。
到目前为止,我对此感到很困惑,所以我们将不胜感激,谢谢。
with cte (id, title, Name) as
(
select id, title, jsonb_array_elements(info->'Characters')->>'Name' as Name
from books
where (info->>'Characters')::text is not null
)
select id, title, Name
from cte
where Name like 'fo%';
如标题所述,我正在尝试执行一个查询,该查询对多行进行全文搜索,再次是 jsonb 数据类型,带有嵌套数据,问题如下:
CREATE TABLE books (id int primary key, title text, info jsonb);
INSERT INTO users (id, t, j) VALUES
(1, 'title 1', '{"Characters": [{"Name": "foo"}]}'),
(2, 'title 2', '{"Characters": [{"Name": "foo"},{"Name": "bar"}]}');
(3, 'title 3', '{"Characters": null}');
问题:
我想通过每本书中人物的名字查询书籍。例如查询每本书都有一个名为 "foo" 的字符。虽然上面的大纲只是一个例子,但我的现实生活场景要求我使用“~*”运算符搜索角色的名字。
到目前为止,我对此感到很困惑,所以我们将不胜感激,谢谢。
with cte (id, title, Name) as
(
select id, title, jsonb_array_elements(info->'Characters')->>'Name' as Name
from books
where (info->>'Characters')::text is not null
)
select id, title, Name
from cte
where Name like 'fo%';