PostgreSQL的 - 文本数组包含类似值 [英] PostgreSQL - text Array contains value similar to

查看:512
本文介绍了PostgreSQL的 - 文本数组包含类似值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让那里类型的列文本[] 包含类似的一些用户输入一个值的行。

我已经想到,到目前为止所做的是使用任意LIKE 运算符像这样的:

  SELECT * FROM someTable其中,'%someInput%'喜欢的任何(someColum);

但它不工作。该查询返回相同的值,这个查询:

  SELECT * FROM someTable其中someInput'= ANY(someColum);

我已经得到了很好的使用子查询的 UNNEST()函数的结果,但我需要在WHERE子句如果可能的查询这一点。

为什么不与 LIKE 运营商工作任何运营商,我没有得到任何错误?我认为其中一个原因应该是任何运营商在查询的右手,但是......

有没有解决这个不使用 UNNEST(),如果有可能在 WHERE 条款<? / p>

解决方案

同样重要的是要明白,任何的一个运营商而是一个SQL构造,只能用来给的右键的操作的。更多:

LIKE 运营商 - 或者更precisely:前pression 的,即改写为 运营商在内部的Postgres - 预计的的左侧和的模式的右侧。没有 换向器 这个操作符(像有很简单等于运算符 = ),所以Postgres不能只是前后翻页操作数。

您尝试:

  SELECT * FROM someTable其中,'%someInput%'喜欢的任何(someColum);

已经翻转,左右操作数,使得'%someInput%的和数组列的元素 someColum 取为模式(这是不是你想要的)。

它的将会的必须的 ANY(someColum)LIKE'%someInput% 的 - 除了这不可能与任何结构是只允许在右键的操作的。您在这里创下了路障。

相关阅读:

UNNEST()是解决方案,因为你已经找到自己 - 或者保存的元素的,而不是一个的阵列的开始与(标准化的模式)。

I'm trying to get rows where a column of type text[] contains a value similar to some user input.

What I've thought and done so far is to use the 'ANY' and 'LIKE' operator like this:

select * from someTable where '%someInput%' LIKE ANY(someColum);

But it doesn't work. The query returns the same values as that this query:

select * from someTable where 'someInput' = ANY(someColum);

I've got good a result using the unnest() function in a subquery but I need to query this in WHERE clause if possible.

WHY doesn't the LIKE operator work with the ANY operator and I don't get any errors? I thought that one reason should be that ANY operator is in the right-hand of query, but ...

Is there any solution to this without using unnest() and if it is possible in WHERE clause?

解决方案

It's also important to understand that ANY is not an operator but an SQL construct that can only be used to the right of an operator. More:

The LIKE operator - or more precisely: expression, that is rewritten with to the ~~ operator in Postgres internally - expects the value to the left and the pattern to the right. There is no COMMUTATOR for this operator (like there is for the simple equality operator =) so Postgres cannot flip operands around.

Your attempt:

select * from someTable where '%someInput%' LIKE ANY(someColum);

has flipped left and right operand so '%someInput%' is the value and elements of the array column someColum are taken to be patterns (which is not what you want).

It would have to be ANY(someColum) LIKE '%someInput%' - except that's not possible with the ANY construct which is only allowed to the right of an operator. You are hitting a road block here.

Related:

unnest() is the solution, as you already found yourself - or save elements instead of an array to begin with (normalize the schema).

这篇关于PostgreSQL的 - 文本数组包含类似值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆