13

我有以下 jsonb 结构:

{'this': '1', 'this_that': '0', 'this_and_that': '5'}

如何选择包含 LIKE 运算符的行?

SELECT * FROM myjson WHERE j ? 'this_%'

返回 0 行...希望它能匹配 'this_that' 和 'this_and_that'。(注意:“_”后面的字符可能会有很大差异,因此我无法进行精确匹配)。

4

1 回答 1

14

jsonb您的示例不应该工作,因为andtext类型之间没有隐式转换。您可以强制强制转换:

SELECT '{"this": 1, "this_that": 0, "this_and_that": 5}'::jsonb::text 
            like '%"this%';

这不是干净的解决方案。更好的是解包 json,并使用横向连接过滤解包数据

postgres=# SELECT key FROM  myjson, lateral jsonb_each_text(j) 
             WHERE key LIKE 'this\_%';
┌───────────────┐
│      key      │
╞═══════════════╡
│ this_that     │
│ this_and_that │
└───────────────┘
(2 rows)
于 2015-05-14T04:53:59.137 回答