postgresql - 如何从 postgres 中正确获取特定键的值。同时删除 null
问题描述
填充数据。
Begin;
CREATE TEMPORARY TABLE test (id serial, jdoc jsonb);
insert into test(jdoc) values('{"a": {"b":"foo"}}');
insert into test(jdoc) values('{"a": "test"}');
insert into test(jdoc) values('{"a":[2,3,4]}');
insert into test(jdoc) values('{"b":[2,3,4]}');
commit;
基于这个答案, SELECT jsonb_object_agg(key, value) FROM jsonb_each(SELECT (jdoc->'a') from test) WHERE key IN ('a'));
但是这个查询不起作用。遇到错误。
SELECT (jdoc->'a') from test;
将过滤掉包含键'a'的值。但是这个查询返回 4 行。预计只有 3 行。因为只有 3 行符合条件。那么如何正确地从 postgres 中获取特定键的 jsonb 呢?
解决方案
如果您只想获取包含键的行,请a
使用?
运算符
select *
from test
where jdoc ? 'a'
推荐阅读
- java - 为什么 Files.readAllBytes() 不接受编码参数?
- python-3.x - 如何在 PythonOCC 中将 3d 盒子缩小为 2d 正方形
- c# - 使用 C# 泛型方法并使其成为强类型
- python - 如何在 Python 中修复 while 循环
- php - 在 laravel 中使用 morphTo() 多对多多态
- android - Android:更改 ProgressBar 可见性不起作用
- python - 如何将具有相同索引的两个单独列表的组合元素插入另一个列表?
- preact - 如何更改导航栏徽标 img、背景和链接颜色?
- r - R 在 summarise_at 中应用 2 个不同的函数
- elasticsearch - 如何从 Kibana TSVB 显示 2 个月的平均值?