需要检查 jsonb 列是否包含值。 Jsonb 字段是一个简单的数组:
ALTER TABLE task ADD COLUMN worker_ids jsonb;
UPDATE task SET worker_ids = '["1", "2"]' WHERE ...
然后尝试运行查询来检查它是否包含具体值:
检查 jsonb 是否包含值的查询 - 工作正常并返回正确的行集
SELECT * FROM task WHERE worker_ids ? '1'
然而,当我将 NOT
添加到 where 条件查询时,什么都不返回:
SELECT * FROM task WHERE NOT worker_ids ? '1'
我是不是弄错了什么?
最佳答案
https://www.postgresql.org/docs/current/static/functions-comparison.html
Ordinary comparison operators yield null (signifying “unknown”), not true or false, when either input is null. For example, 7 = NULL yields null, as does 7 <> NULL. When this behavior is not suitable, use the IS [ NOT ] DISTINCT FROM predicates:
类似于:
SELECT * FROM task WHERE (worker_ids ? '1') is distinct from true
应该可以
关于sql - Postgres 检查 jsonb 数组是否不包含值返回空结果集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49940344/