设置(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;
这可能吗?我可以看到,如果你在同一个对象上有颜色和颜色键可能会有冲突,所以如果这不是一件事我也不会感到惊讶.
参考文献:
你应该提取对(键,值)来使用函数lower()
select value as color from product,jsonb_each(attributes) where lower(key) = 'color';
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。