代码之家  ›  专栏  ›  技术社区  ›  Vishal Gajera

org.postgresql.util.PSQLException:错误:在其中不允许使用set返回函数

  •  0
  • Vishal Gajera  · 技术社区  · 7 年前

    我需要一个关于下面提到的细节的帮助,

    我正在使用Postgres+Spring数据JPA。此外,我还使用了 jsonb

    我试图执行一个查询,但它给出了以下错误:

    ERROR: set-returning functions are not allowed in WHERE
    

    原因是我加了一个 jsonb公司 中的条件 WHERE 子句(有关详细信息,请参阅下面的查询)。

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

    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获取数据吗?尤其是在我的例子中,我有一个内部列表和一些内部元素。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Laurenz Albe    7 年前

    我建议横向连接 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" }
                                ]
            }');
    

    找到所有 id 其中关联的JSON包含一个数组元素 action = PENDING

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