arrays - 获取值为 true 的 postgresql 表中 jsonb 对象的所有键
问题描述
我有一个customers
带有name
和features
列的 postgresql 表。
features
包含 jsonb 对象,例如{"featureA": true, "featureB": false, "featureC":true}
我想得到的是这些键的数组,features
其中每个键的值都为真name
,例如:
name | features
----------|---------------------
customerA | [featureA, featureC]
customerB | [featureB, featureC]
从这篇文章中,我了解到
SELECT key
FROM jsonb_each()
WHERE value = jsonb 'true'
是你如何获得真实的钥匙,但我该如何为我的桌子做到这一点customers
?
就像是
SELECT array_agg(key)
FROM jsonb_each((select features from customers))
WHERE value = jsonb 'true'
返回SQL Error [21000]: ERROR: more than one row returned by a subquery used as an expression
。
任何帮助,将不胜感激。
解决方案
您正在描述横向连接:
select c.name, x.keys
from customers c
cross join lateral (
select array_agg(x.key) keys
from jsonb_each(c.features) x
where x.value = jsonb 'true'
) x
推荐阅读
- rust - 如何指定在某些特征边界下满足给定第一个特征的任何类型也必须隐式满足第二个特征?
- python - 是否可以防止 VISUAL STUDIO 在 DEBUG CONSOLE 中自动添加新行?
- wso2 - BPMN 集成?
- r - 如何在 valueBox (flaxdashboard R) 中插入图标?
- javascript - HTML5 视频:无法读取未定义的属性“toLowerCase”
- python-3.x - 如何修改 PostgreSQL psycopg 以接受变量而不是值
- spring - 使用 Spring Boot 在匿名 url 上接收授权标头
- apache-spark - Spark结构化流式连接不起作用
- elixir - 如何在另一个模块中使用结构
- terraform - local-exec 中的相对路径