PostgreSQL对数组不区分大小写的SELECT

编程入门 行业动态 更新时间:2024-10-25 21:27:12
本文介绍了PostgreSQL对数组不区分大小写的SELECT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在Google或文档中无法找到答案... 我需要对数组类型进行不区分大小写的选择。

I'm having problems finding the answer here, on google or in the docs ... I need to do a case insensitive select against an array type.

因此,如果:

value = {"Foo","bar","bAz"}

我需要

SELECT value FROM table WHERE 'foo' = ANY(value)

中选择值。

我尝试了很多Lower()组合,但均未成功。

I've tried lots of combinations of lower() with no success.

用ILIKE 代替 = 似乎有效,但我一直对 Like 感到紧张-是最好的方法吗?

ILIKE instead of = seems to work but I've always been nervous about LIKE - is that the best way?

推荐答案

一个未提及的替代方法是安装 PostgreSQL 8.4+随附的 citext 扩展名,并使用数组 citext :

One alternative not mentioned is to install the citext extension that comes with PostgreSQL 8.4+ and use an array of citext:

regress=# CREATE EXTENSION citext; regress=# SELECT 'foo' = ANY( '{"Foo","bar","bAz"}'::citext[] ); ?column? ---------- t (1 row)

如果您想严格正确地避免扩展,则必须做一些非常难看的子查询,因为Pg没有很多丰富的数组操作,特别是没有功能映射操作。像这样的东西:

If you want to be strictly correct about this and avoid extensions you have to do some pretty ugly subqueries because Pg doesn't have many rich array operations, in particular no functional mapping operations. Something like:

SELECT array_agg(lower(($1)[n])) FROM generate_subscripts($1,1) n;

...其中$ 1是数组参数。在您的情况下,我认为您可以作弊,因为您不关心保留数组的顺序,因此您可以执行以下操作:

... where $1 is the array parameter. In your case I think you can cheat a bit because you don't care about preserving the array's order, so you can do something like:

SELECT 'foo' IN (SELECT lower(x) FROM unnest('{"Foo","bar","bAz"}'::text[]) x);

更多推荐

PostgreSQL对数组不区分大小写的SELECT

本文发布于:2023-10-20 09:04:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1510531.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   大小写   PostgreSQL   SELECT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!