Skip to content
Advertisement

How can I add condition based on select value (JSONB) on PostgreSQL 12?

I want to filter rows that have gold badges. I am using PG12 and I use new path feature of it.

To clarify, I have a users_tbl table like this:

And lets insert values:

Now when I query like this:

I get following result as expected:

Now list only have badge matches gold

Expected result:

Actual Result:

How can I add condition against badges? Or am I doing something not correct? How can I get expected result in my case?

Note: PostgreSQL 12.

Update:

Actually in this example I’ve used a simple jsonb object. In reality its like this:

So, badges are in properties

Advertisement

Answer

You can use the containment operator:

or jsonb_path_exists():

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement