postgresql jsonb case insensitive query

倖福魔咒の 提交于 2021-02-08 13:45:24

问题


I have a table like:

CREATE TABLE cityData
(
  item character varying,
  data jsonb
);

it contains values like

ITEM        DATA
test1       [{"rank":"1", "city":"New York"},{"rank":"3", "city":"Sidney"}] 
test2       [{"rank":"2", "city":"NEW YORK"},{"rank":"4", "city":"New Delhi"}] 

I need to get number of distinct json objects where city is 'New York' I am using the following query

SELECT  * FROM   cityData t
WHERE ( data @> '[{"city":"New York"}]')
and t.item ilike '%test%';

But this query outputs test1 row. I need to make the query case insensitive so that data @> '[{"city":"New York"}]' matches both New York and NEW YORK


回答1:


where lower(data::text)::jsonb @> lower('[{"city":"New York"}]')::jsonb


来源:https://stackoverflow.com/questions/37949620/postgresql-jsonb-case-insensitive-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!