postgresql返回jsonb对象的计数

时间:2015-10-09 13:02:00

标签: postgresql jsonb

我想得到一定数量的所有产品的数量。

我试过了:

SELECT
    count(jsonb_object_keys(data #> '{products}')) as numProducts 
FROM
    appointment_intakes 
WHERE
    appointment_intakes.id = 'VGDKMjdxn'

我明白了:

ERROR:  set-valued function called in context that cannot accept a set

当我在没有count功能的情况下运行上述内容时,我会得到一个键列表。

知道我做错了吗?

谢谢。

1 个答案:

答案 0 :(得分:2)

必须在FROM子句中调用集值函数。使用横向连接:

SELECT
    count(keys) as numProducts 
FROM
    appointment_intakes, 
    LATERAL jsonb_object_keys(data #> '{products}') keys
WHERE
    appointment_intakes.id = 'VGDKMjdxn'