简体   繁体   English

Postgres查询JSON内的数组元素?

[英]Postgres query array elements inside JSON?

I have Postgres 11 table called fb_designs that has a json column with data structured like so:我有一个名为 fb_designs 的 Postgres 11 表,它有一个json列,其数据结构如下:

{
    "listings": [
       {
            "id": "KTyneMdrAhAEKyC9Aylf",
            "active": true
        },
        {
            "id": "ZcjK9M4tuwhWWdK8WcfX",
            "active": false
        }
    ]
}

and a tags column in a character varying[] format as so {dWLaRWChaThFPH6b3BpA,BrYiPaUiou020hsmRugR} .和一个character varying[]格式的tags列,如{dWLaRWChaThFPH6b3BpA,BrYiPaUiou020hsmRugR} Both lengths are undefined.两个长度都是未定义的。

What I am trying to do is produce a some queries that will let me say in laymans terms,我想做的是产生一些查询,让我用外行的话来说,

show me all results where at all items.listings has an active status and tags contains BrYiPaUiou020hsmRugR显示所有 items.listings 处于活动状态且标签包含BrYiPaUiou020hsmRugR的所有结果

I got this far, however I'm not sure how to add in WHERE uid = "foo" , WHERE tags contains "foo", "bar and WHERE title is like %hoot%我已经到了这一步,但是我不确定如何添加WHERE uid = "foo"WHERE tags contains "foo", "barWHERE title is like %hoot%

SELECT id, title, tags, selected_preview_image, items
FROM  fb_designs r, json_array_elements(r.items#>'{listings}') obj 
WHERE  obj->>'active' = 'true' 
GROUP BY id

If they are all true, then none of them are false.如果它们都是真的,那么它们都不是假的。 Sounds like you want to negate the containment operation over false.听起来您想否定包含错误的遏制操作。

select * from fb_designs where 
    not items::jsonb @> '{"listings":[{"active": false}]}'
    and tags && ARRAY['BrYiPaUiou020hsmRugR']::varchar[]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM