【发布时间】:2021-05-28 18:25:18
【问题描述】:
标题可能听起来不太清楚,所以这里是一个示例场景。
这是一个名为test的表。
id | json (this is "jsonb")
----+----------------------
1 | [0]
2 | [1, 2, 3]
3 | [4]
4 | ["4"]
5 | ["5", "6"]
6 | ["5", "6", "7", "8"]
现在,您必须选择json 包含数组中至少一个元素的行。
如果你运行select * from test where json::jsonb ?| array['4', '8', '10'];...
id | json
----+----------------------
4 | ["4"]
6 | ["5", "6", "7", "8"]
如您所见,ID 3 的行被遗漏了。这是因为 ID 为 3 的json 是一个整数数组。
如果你把上面的语句改写成select * from test where json::jsonb ?| array[4, '8', '10'];...
ERROR: operator does not exist: jsonb ?| integer[]
LINE 1: select * from test where json::jsonb ?| array[4, '8', '10'];
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
?| 运算符似乎与整数不兼容。
是否可以选择jsonb(由整数和文本混合而成)与数组重叠的行?我从the doc 找不到任何功能来实现这一点。
我不是数据库专家,所以任何想法都将不胜感激!
【问题讨论】:
-
这些值是否总是数字?可能是文本格式
-
您使用 Postgres 12 或更高版本吗? JSON path 应该可以轻松搞定
标签: postgresql