1

作为测试,我创建了这个模式:

CREATE TABLE simple_table (client_id int4, order_id int4);
INSERT INTO simple_table (client_id, order_id) 
VALUES 
(1,2),(1,3),(1,4),(1,6),(1,8),(1,12),(1,16),(1,18),(1,25),(1,32),(1,33),(1,37),(1,43),
(1,56),(1,57),(1,66),(2,2),(2,3),(2,5),(2,7),(2,9),(2,12),(2,17),(2,19),(2,22),(2,30),
(2,33),(2,38),(2,44),(2,56),(2,58),(2,66)
;



然后使用array_agg:

SELECT client_id, array_agg(order_id) FROM simple_table GROUP BY client_id;



为客户端 1 和客户端 2 创建数组:

| CLIENT_ID |                                  ARRAY_AGG |
----------------------------------------------------------
|         1 | 2,3,4,6,8,12,16,18,25,32,33,37,43,56,57,66 |
|         2 | 2,3,5,7,9,12,17,19,22,30,33,38,44,56,58,66 |



现在我想比较这两行并确定相似的值。从 Postgresql 文档中尝试过&& overlap (have elements in common) ARRAY[1,4,3] && ARRAY[2,1],但我遇到了问题。

也许我看错了。任何帮助或指导将不胜感激!

4

2 回答 2

2

The && operator is a predicate that yields a true or false result, not a list of values.

If you're looking for the list of order_id that exist for both client_id=1 and client_id=2, the query would be:

 select order_id from simple_table  where client_id in (1,2)
  group by order_id having count(*)=2;

That's equivalent to the intersections of the two arrays if you consider that these arrays are sets (no duplicates and the positions of the values are irrelevant), except that you don't need to use arrays at all, simple standard SQL is good enough.

于 2013-07-02T22:38:13.643 回答
0

看看这里的“array_intersect”函数: Array Intersect

要查看两个数组共有的元素:

create or replace function arrxor(anyarray,anyarray) returns anyarray as $$
select ARRAY(
        (
        select r.elements
        from    (
                (select 1,unnest($1))
                union all
                (select 2,unnest($2))
                ) as r (arr, elements)
        group by 1
        having min(arr) = max(arr)
        )
)
$$ language sql strict immutable;
于 2013-07-02T22:39:07.583 回答