【发布时间】:2020-12-13 11:54:33
【问题描述】:
我有一个带有 name 和 features 列的 postgresql 表 customers。
features 包含 jsonb 对象,例如 {"featureA": true, "featureB": false, "featureC":true}
我想要得到的是 features 中的这些键的数组,其中每个 name 的值都为 true,例如:
name | features
----------|---------------------
customerA | [featureA, featureC]
customerB | [featureB, featureC]
从this post得知
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。
任何帮助将不胜感激。
【问题讨论】:
标签: arrays json postgresql unnest lateral-join