0

我的表中有 JSON 列,其中包含字典数组。该数组具有标准格式。

[{'path': 'chat/xyz.pdf', 'file_name': 'xyz.pdf'},
 {'path': 'chat/xyl.pdf', 'file_name': 'xyl.pdf'}]

表名是聊天,列名是附件。我想对文件名执行搜索,这样即使我输入了一个字母,也应该检索该行。例如:如果我按字符串“pd”搜索,则应检索所有带有“pd”字符串的文件名的值。

4

2 回答 2

0

我试过了,它确实有效。

select distinct attachments from chat, jsonb_array_elements_text(attachments)
where value::json->>'file_name' like '%xyz%';

我从文档中获取了参考。

于 2020-03-24T05:50:08.217 回答
0

您可以使用 EXISTS 条件,则不需要 DISTINCT:

select c.*
from chat c
where exists (select *
              from jsonb_array_elements(c.attachments) as t(doc)
              where t.doc ->> 'file_name' like '%xyz%');
于 2020-03-24T06:04:35.223 回答