使用json数据类型查询Postgres(当json数组中没有键时)

时间:2015-11-16 11:32:19

标签: sql json postgresql

我有一张桌子如下图所示。数据的数据类型是json(json没有键,只有值数组)

POST

如果列数据包含“abc@xyz.com”,我想写一个查询来选择所有行

3 个答案:

答案 0 :(得分:1)

使用json_array_elements() 解压缩一个json数组:

select guid, type, json_array_elements(data) elem
from guids;

                 guid                 | type  |     elem      
--------------------------------------+-------+---------------
 9cf100e8-87a8-4ce7-b187-b618bf2dc156 | email | "abc@xyz.com"
 03d5b41c-b834-4399-95dc-c51b1e214fb3 | email | "abc@xyz.com"
(2 rows)

使用派生表过滤数据:

select *
from (
    select guid, type, json_array_elements(data)::text elem
    from guids
    ) sub
where elem = '"abc@xyz.com"';

                 guid                 | type  |     elem      
--------------------------------------+-------+---------------
 9cf100e8-87a8-4ce7-b187-b618bf2dc156 | email | "abc@xyz.com"
 03d5b41c-b834-4399-95dc-c51b1e214fb3 | email | "abc@xyz.com"
(2 rows)

答案 1 :(得分:1)

怎么样

select * from tbl where data::text like '%abc@xyc.com%'

答案 2 :(得分:0)

你可以通过 json_array_elements_text() 来解包,而不是通过 json_array_elements() 解包