PG中文社区 /
mdi-home
首页 社区新闻 中文文档 加入ACE {{ item.text }} 登录
mdi-home 首页 mdi-chat-processing 社区新闻 mdi-book-open-variant 中文文档 mdi-account-multiple-check 加入ACE mdi-file-multiple-outline 相关资料 mdi-blank {{item.text}} mdi-exit-to-app 退出账号
PostgreSQL 数组忽略大小写匹配

原作者:digoal/德哥  创作时间:2016-11-19 22:22:48+08  
doudou586 发布于2016-11-22 22:22:48           评论: 1   浏览: 13929   顶: 4858  踩: 4896 

PostgreSQL 数组忽略大小写匹配

作者: digoal

日期: 2016-11-19

标签: PostgreSQL , 开发 , 数组 , any匹配 , 忽略大小写


背景

一位兄弟的开发需求,要求不区分大小写,匹配数组内的字符串。

如下,这样的匹配。

postgres=# select 'a' = any(array['A','1']);
 ?column? 
----------
 f
(1 row)

需要将数组内的字符串转换为小写后匹配。

postgres=# select 'a' = lower( any(array['A','1']) );
ERROR:  syntax error at or near "any"
LINE 1: select 'a' = lower(any(array['A','1']));
                           ^
postgres=# select 'a' = any( lower(array['A','1']) );
ERROR:  function lower(text[]) does not exist
LINE 1: select 'a' = any(lower(array['A','1']));
                         ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

在PostgreSQL中这个需求还是很好实现的,例如加操作符,或者UDF都可以。

通过操作符实现忽略大小写的数组元素匹配

添加一个基础函数,返回忽略大小写的比较结果。

postgres=# create function test_ci_compare(text,text) returns boolean as $$
postgres$# select lower($1)=lower($2);
postgres$# $$ language sql strict immutable;
CREATE FUNCTION

postgres=# select test_ci_compare('a','A');
 test_ci_compare 
-----------------
 t
(1 row)

使用这个函数创建操作符,这个操作符就是忽略大小写的了。

postgres=# create operator ~= (procedure = test_ci_compare(text,text) , LEFTARG='text', rightarg='text');
CREATE OPERATOR

原来的SQL改写成如下,使用新的操作符即可

postgres=# select 'a' ~= any(array['A','1']);
 ?column? 
----------
 t
(1 row)

这种方法可能不适用于索引扫描,除非你连索引OPAM也一起添加好。

UDF,对数组进行转换的方式

新增一个UDF,将数组内的元素转换为小写。

postgres=# create or replace function lower(text[]) returns text[] as $$
select array_agg(lower(x)) from unnest($1) t(x);
$$ language sql strict immutable;
CREATE FUNCTION

postgres=# select lower(array['A','a']);
 lower 
-------
 {a,a}
(1 row)

这样,就可以愉快的使用忽略大小写的匹配了,还可以继续使用索引。 postgres=# select 'a' = any( lower(array['A','1']) ); ?column? ---------- t (1 row)

一个简单的CASE,希望可以帮助到你。

/images/news/2016/pg_bot_banner.jpg


评论:1   浏览: 13929                   顶: 4858  踩: 4896 

请在登录后发表评论,否则无法保存。

1# __ xiaowu 回答于 2024-04-22 09:28:12+08
古风仙气昵称:https://www.nanss.com/mingcheng/2252.html 将才读后感:https://www.nanss.com/xuexi/2129.html 雅思词汇量:https://www.nanss.com/xuexi/2083.html 三分钟演讲稿:https://www.nanss.com/xuexi/2369.html 赞美小朋友跳舞好的句子:https://www.nanss.com/yulu/1889.html 探险作文:https://www.nanss.com/xuexi/2190.html 慢生活悠闲惬意句子:https://www.nanss.com/yulu/2384.html wow名字:https://www.nanss.com/mingcheng/1827.html 天龙八部女名字:https://www.nanss.com/mingcheng/2277.html 消息类新闻范文案例:https://www.nanss.com/xuexi/2425.html 男友18岁生日留言长篇:https://www.nanss.com/yuedu/1767.html 我的孤独:https://www.nanss.com/xuexi/2158.html 四字文案:https://www.nanss.com/wenan/2034.html 高端局伤感id:https://www.nanss.com/mingcheng/2350.html 天气很好阳光很美句子:https://www.nanss.com/yulu/2375.html 大学自我鉴定:https://www.nanss.com/xuexi/2271.html 绝望的句子八个字:https://www.nanss.com/yulu/1730.html 消防安全责任书:https://www.nanss.com/gongzuo/2115.html 节约粮食作文:https://www.nanss.com/xuexi/2311.html 浪漫的鲜花寄语:https://www.nanss.com/yulu/1770.html 让真情流露作文500字:https://www.nanss.com/xuexi/2064.html 霸道总裁名字:https://www.nanss.com/mingcheng/2406.html 疫情励志句子唯美简短:https://www.nanss.com/yulu/1884.html 有诗意又儒雅的团队名字:https://www.nanss.com/gongzuo/2346.html 走在小巷里心情好与不好:https://www.nanss.com/xuexi/2014.html 时光太匆匆的经典句子:https://www.nanss.com/yulu/2031.html 亏欠父母太多经典感言:https://www.nanss.com/yulu/2292.html 收到礼物高情商发朋友圈句子:https://www.nanss.com/wenan/2408.html 签约仪式主持词:https://www.nanss.com/gongzuo/2302.html 关系很铁的群名:https://www.nanss.com/mingcheng/2334.html



发表评论:
加入我们
QQ群1:5276420
QQ群2:3336901
QQ群3:254622631
文档群:150657323
文档翻译平台:按此访问
社区邮件列表:按此订阅
商业支持
扫码关注
加入我们
QQ群1:5276420
QQ群2:3336901
QQ群3:254622631
文档群:150657323
文档翻译平台:按此访问
社区邮件列表:按此订阅
商业支持
扫码关注
© PostgreSQL中文社区 ... (自2010年起)