【问题标题】:org.postgresql.util.PSQLException: ERROR: set-returning functions are not allowed in WHEREorg.postgresql.util.PSQLException:错误:在 WHERE 中不允许设置返回函数
【发布时间】:2019-04-16 23:37:57
【问题描述】:

下面提到的细节我需要帮助,

我正在使用 Postgres + Spring-Data-JPA。此外,我使用了jsonb 数据类型来存储数据。

我正在尝试执行查询,但它给了我以下错误:

ERROR: set-returning functions are not allowed in WHERE

这里的原因是我在WHERE 子句中添加了jsonb 条件(请参阅下面的查询以获取更多详细信息)。

查询(我重命名了列名只是因为隐藏了实际的列名):

select distinct
    jsonb_array_elements(initiated_referral_detail->'listOfAttribue')::jsonb
        ->>  'firstName' as firstName,
    jsonb_array_elements(initiated_referral_detail->'listOfAttribue')::jsonb
        ->>  'lastName' as lastName,
    jsonb_array_elements(initiated_referral_detail->'listOfAttribue')::jsonb
        ->>   'country' as country  
from
    tale1 table10_ 
left outer join
    table2 table21_ 
        on table10_.end_user_id=table21_.end_user_id 
left outer join
    table3 table32_ 
        on table10_.manufacturer_id=table32_.manufacturer_id  
where
    table21_.end_user_uuid=(
        ?
    ) 
    and table21_.is_active=true 
    and table32_.manufacturer_uuid=(
        ?
    ) 
    and table32_.is_active=true 
    and table10_.is_active=true 
    and table32_.is_active=true 
    and jsonb_array_elements(initiated_referral_detail->'listOfAttribue')::jsonb
        ->>  'action' = ('PENDING') 
order by
    jsonb_array_elements(initiated_referral_detail->'listOfAttribue')::jsonb
        ->>  'firstName',
    jsonb_array_elements(initiated_referral_detail->'listOfAttribue')::jsonb
        ->>  'lastName'
limit ?

上述查询中的以下行导致错误:

and jsonb_array_elements(initiated_referral_detail->'listOfAttribue')::jsonb
    ->> 'action' = ('PENDING')

谁能指导我如何从内部 JSON 中获取数据?特别是在我的情况下,我有一个内部列表和一些元素。

【问题讨论】:

  • 错误信息似乎很清楚。真正的问题是什么?
  • @JensSchauder 我不清楚这个错误。可能是新手。 #2。我想更多地了解如何添加用于嵌套 JSON 元素的 where 子句条件。如果您有任何链接/文章,请与我/我们分享以进一步澄清。
  • 这里不是专家,但这可能会有所帮助:stackoverflow.com/questions/28486192/…

标签: postgresql spring-boot spring-data-jpa spring-data jsonb


【解决方案1】:

对于这种情况,我建议使用jsonb_array_elements 横向连接。这是一个例子:

CREATE TABLE tale1 (
   id integer PRIMARY KEY,
   initiated_referral_detail jsonb NOT NULL
);

INSERT INTO tale1 VALUES
   (1, '{
          "name": "one",
          "listOfAttribue": [
                              { "id": 1, "action": "DONE"},
                              { "id": 2, "action": "PENDING" },
                              { "id": 3, "action": "ACTIVE" }
                            ]
        }');

INSERT INTO tale1 VALUES
   (2, '{
          "name": "two",
          "listOfAttribue": [
                              { "id": 1, "action": "DONE"},
                              { "id": 2, "action": "ACTIVE" }
                            ]
        }');

要查找所有ids,其中关联的JSON包含action = PENDING的数组元素,您可以这样查询:

SELECT DISTINCT id
FROM tale1 CROSS JOIN LATERAL
     jsonb_array_elements(initiated_referral_detail -> 'listOfAttribue') AS attr
WHERE attr ->> 'action' = 'PENDING';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-21
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 2020-04-10
    • 2015-04-23
    相关资源
    最近更新 更多