json - 如何对 jsonb 键执行 LIKE 查询?

标签 json postgresql jsonb

我有以下 jsonb 结构:

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

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

SELECT * FROM myjson WHERE j ? 'this_%'

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

最佳答案

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

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)

关于json - 如何对 jsonb 键执行 LIKE 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30228984/

相关文章:

android - 将 JSON 解析为 ListView 友好的输出

json - YouTube视频更新日期

json - 如何在 Artillery.i.o 中运行 Node-js 脚本而不将其包含在负载测试场景中

java - 通过 JDBC 的 Postgres 连接 : org. postgresql.util.PSQLException: ERROR: relation "prescriptions"does not exist

database - 如何使用 vb.net 备份 PostgreSQL

ruby-on-rails - 事件记录 postgres 检查 jsonb 数组是否包含 x 或 y

php - 如何在 PHP 中进行变量缓存

sql - 如何更改postgresql中的外键值?

json - 使用 PostgreSQL 和 JSONB 选择 "WHERE IN"

json - 从同一表中的其他列更新 JSONB 列数据