设置(PostgreSQL 9.4+)
假设我有一个表product
:
create table product
(
attributes jsonb
);
使用数据:
insert into product (attributes)
values ('{"Color": "Red"}'),
('{"color": "White"}'),
('{"COLOR": "Blue"}');
问题
如何在PostgreSQL 9.4+中选择所有记录的color
属性?由于键的大小写不同,我无法使用此语法:
select
attributes->>'color' as color
from product;
我的期望输出为:
Red
White
Blue
可能的解决方案
我也尝试使用这个语法(虽然能用但感觉有点取巧):
select
coalesce(
attributes->>'color',
attributes->>'Color',
attributes->>'COLOR') as color
from product;
这可能吗?如果您在同一个对象上拥有color
和Color
键,那么可能会发生冲突,所以如果这不是一件事情,我也不会感到惊讶。
参考资料: