postgres - 比较两个数组

pst*_*ton 18 sql arrays postgresql

postgres有一个数组数据类型,在本例中是一个数字数组:

CREATE TABLE sal_emp (name text, pay_by_quarter integer[]);
INSERT INTO sal_emp VALUES ('one', '{1,2,3}');
INSERT INTO sal_emp VALUES ('two', '{4,5,6}');
INSERT INTO sal_emp VALUES ('three', '{2,4,6}');
SELECT * FROM sal_emp;

Result:
one, {1,2,3}
two, {4,5,6}
three, {2,4,6}
Run Code Online (Sandbox Code Playgroud)

据我所知,您只能按如下方式查询数组:

SELECT * FROM sal_emp WHERE 4=ANY(pay_by_quarter);
SELECT * FROM sal_emp WHERE ARRAY[4,5,6]=pay_by_quarter;
Run Code Online (Sandbox Code Playgroud)

这意味着您可以选择一个数组包含单个参数匹配的行,或者整个数组是否与数组参数匹配.

我需要选择一行,其中行的数组的任何成员都匹配参数数组的任何成员 - 有点像'IN',但我无法弄清楚如何.我尝试了以下两种方法,但都没有工作:

SELECT * from sal_emp WHERE ARRAY[4,5,6]=ANY(pay_by_quarter);
SELECT * from sal_emp WHERE ANY(pay_by_quarter) IN (4,5,6);
Run Code Online (Sandbox Code Playgroud)

我假设我可以做一些事情将数组转换为字符串,但这听起来像是糟糕的解决方案..

有任何想法吗?

pst*_*ton 24

想通了......有一个&&运营商

http://www.postgresql.org/docs/8.2/static/functions-array.html

"&&重叠(共有元素)ARRAY [1,4,3] && ARRAY [2,1]"


Ani*_*air 12

为了在 Postgress 中比较数组,有数组运算符。
在这里找到“运算符”部分:https ://www.postgresql.org/docs/current/functions-array.html

比较两个数组的快速方法是这样做,

( (ARRAY_1 @> ARRAY_2) AND (ARRAY_1 <@ ARRAY_2) )
Run Code Online (Sandbox Code Playgroud)

它基本上检查 ARRAY_2 中的所有唯一值是否都存在于 ARRAY_1 中,AND反之亦然。不要忘记结合使用检查/条件/比较( AND)

这些称为包含运算符,它检查一个数组中的所有唯一值并将其与另一个数组进行比较。它们也被称为鸟运算符,因为运算符@>AND<@类似于鸟的头。

其工作方式如下:

ARRAY_1 <@ ARRAY_2
// Check/Direction: Left-to-Right
// Checks if all the unique elements in ARRAY_1 is present in ARRAY_2 as well. If yes, it will return TRUE
// Please note this will return TRUE even if ARRAY_2 has extra elements that are not present in ARRAY_1
Run Code Online (Sandbox Code Playgroud)

相似地,

ARRAY_1 @> ARRAY_2
// Note we have changed the operator to '@>' from '<@'. Check/Direction: Right-to-Left
// Checks if all the unique elements in ARRAY_2 is present in ARRAY_1 as well. If yes, it will return TRUE
// Please note this will return TRUE even if ARRAY_1 has extra elements that are not present in ARRAY_2
Run Code Online (Sandbox Code Playgroud)

因此,即使元素的顺序不同,它仍然有效。

避免混淆并记住这一点的一个简单方法是查看>OR<符号。
指向小于号的 ARRAY(唯一元素)与指向大于号的 ARRAY(唯一元素)进行比较。
因此指向大于号的 ARRAY 可以包含额外的唯一元素,并且仍然返回TRUE。

让我们尝试一些例子

SELECT array[1, 2, 3] @> array[1, 2, 3] AS RESULT
!-- Returns TRUE (Check is Right-to-Left)

SELECT array[1, 2, 3] @> array[3, 1, 2] AS RESULT
!-- Returns TRUE (Check is Right-to-Left)

SELECT array[1, 2, 3, 3, 3, 3] @> array[1, 2, 3] AS RESULT
!-- Returns TRUE (Check is Right-to-Left)

SELECT array[1, 2, 3, 3, 3, 3] <@ array[3, 2, 1, 3, 2, 5] AS RESULT
!-- Returns TRUE (Check is Left-to-Right)

SELECT array[1, 2, 3, 4] <@ array[1, 2, 3] AS RESULT
!-- Returns FALSE (Check is Left-to-Right)

SELECT array[1, 2, 3, 4] @> array[1, 2, 3] AS RESULT
!-- Returns TRUE (Check is Right-to-Left)

SELECT array[1, 2, 3] <@ null AS RESULT
!-- Returns empty (Check is Left-to-Right)

SELECT array[1, 2, 3] @> null AS RESULT
!-- Returns empty (Check is Right-to-Left)

SELECT null <@ null AS RESULT
!-- Doesn't work if both the values are null - Error: Operator is not unique: unknown <@ unknown

SELECT array[1, 2, 3] <@ array[] AS RESULT
!-- Doesn't work on empty arrays - Error: Cannot determine type of empty array

SELECT array[] <@ array[] AS RESULT
!-- Doesn't work on empty arrays - Error: Cannot determine type of empty array
Run Code Online (Sandbox Code Playgroud)

请注意

  • 这用于比较数组。
  • 通过从两个数组中提取唯一值来工作 - 然后比较它们。
  • 它不检查 ARRAY 中元素的计数。即即使两个数组的长度不同([1, 2] @> [1]或[1, 1] @> [1]仍然是TRUE) ,它也可以返回 TRUE
  • 如果两个值都是则不起作用NULL
  • []如果任一数组为空 ( ) ,则不起作用


Kaz*_*Nur 6

要将一个数组的元素与另一个数组的元素进行至少一个匹配的比较,可以使用&&运算符。仅当每个数组中至少有一个元素匹配时,它才会返回 true。

尽管这个问题的正确答案已经存在了几十年,但我添加这个例子是为了让初学者更清楚。

架构:

 create table mytable (id int, value text[]);
 insert into mytable values (1,'{"dog", "cat", "fish"}');
 insert into mytable values (2,'{"elephant", "mouse"}');
 insert into mytable values (3,'{"lizard", "dog", "parrot"}');
 insert into mytable values (4,'{"bear", "bird", "cat"}');
Run Code Online (Sandbox Code Playgroud)

询问:

 select * from mytable where array['dog', 'cat']&&(value);
Run Code Online (Sandbox Code Playgroud)

输出:

ID 价值
1 {狗、猫、鱼}
3 {蜥蜴、狗、鹦鹉}
4 {熊、鸟、猫}

db<>在这里摆弄

Query#2(使用通配符匹配数组元素)

要使用通配符比较数组元素,您可以使用 unnest() 解析两个数组的每个元素,并使用 Like 运算符比较它们。

   SELECT    distinct m.*
   FROM  mytable m, unnest(value)b 
   inner join  unnest(array['dog%', 'ca%']) a 
   on b LIKE a 
Run Code Online (Sandbox Code Playgroud)

输出:

ID 价值
1 {狗、猫、鱼}
3 {蜥蜴、狗、鹦鹉}
4 {熊、鸟、猫}

db<>在这里摆弄