如何在PostgreSQL中选择不区分大小写的JSONB密钥(9.4)
发布时间:2020-12-13 18:04:16 所属栏目:百科 来源:网络整理
导读:设置(PostgreSQL 9.4) 假设我有一个表产品: create table product( attributes jsonb); 有数据: insert into product (attributes) values ('{"Color": "Red"}'),('{"color": "White"}'),('{"COLOR": "Blue"}'); 题 如何在PostgreSQL 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'; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |