Searching jsonb array in PostgreSQL

后端 未结 1 909
萌比男神i
萌比男神i 2021-01-03 01:11

I\'m trying to search a JSONB object in PostgreSQL 9.4. My question is similar to this thread.

However my data structure is slightly different which is causing me pr

1条回答
  •  日久生厌
    2021-01-03 02:00

    You can use exists:

    SELECT * FROM 
        (SELECT 
            '[{"id":1,"msg":"testing"},{"id":2,"msg":"tested"},{"id":3,"msg":"nothing"}]'::jsonb as data) 
        as jsonbexample 
    WHERE 
        EXISTS (SELECT 1 FROM jsonb_array_elements(data) as j(data) WHERE (data#>> '{msg}') LIKE '%est%');
    

    To query table as mentioned in comment below:

    SELECT * FROM atable 
    WHERE EXISTS (SELECT 1 FROM jsonb_array_elements(columnx) as j(data) WHERE (data#>> '{msg}') LIKE '%est%');
    

    0 讨论(0)
提交回复
热议问题