微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在PostgreSQL中选择不区分大小写的JSONB密钥(9.4)

设置(Postgresql 9.4)

假设我有一个表产品:

create table product
(
    attributes jsonb
);

有数据:

insert into product (attributes) 
values ('{"Color": "Red"}'),('{"color": "White"}'),('{"COLOR": "Blue"}');

如何在Postgresql 9.4中选择所有记录的颜色属性?由于键的外壳不同,我无法使用此语法:

select 
    attributes->>'color' as color
from product;

我的预期输出是:

Red
White
Blue

可能解决方

我也试过使用这种语法(工作但感觉很难):

select 
    coalesce(
        attributes->>'color',attributes->>'Color',attributes->>'COLOR') as color 
from product;

这可能吗?我可以看到,如果你在同一个对象上有颜色和颜色键可能会有冲突,所以如果这不是一件事我也不会感到惊讶.

参考文献:

> PostgreSQL JSON Functions and Operators

你应该提取对(键,值)来使用函数lower()
select value as color
from product,jsonb_each(attributes)
where lower(key) = 'color';

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐