这里是另一种选择:
select ARRAY
(
(
select unnest(ARRAY[ 'c', 'd', 'e' ])
except
select unnest(ARRAY[ 'a', 'b', 'c' ])
)
union
(
select unnest(ARRAY[ 'a', 'b', 'c' ])
except
select unnest(ARRAY[ 'c', 'd', 'e' ])
)
);
或(以使其更清晰,两种不同阵列的时候):
with array_one (e) as (
select unnest(ARRAY[ 'a', 'b', 'c' ])
), array_two (e) as (
select unnest(ARRAY[ 'c', 'd', 'e' ])
)
select array(
(
select e from array_one
except
select e from array_two
)
union
(
select e from array_two
except
select e from array_one
)
) t;
如果元素的顺序是非常重要的,那么ARRAY_AGG()需要被用作Clodo阿尔内托为已完成(而不是使用array(...)
构造函数):
with array_one (e) as (
select unnest(ARRAY[ 'a', 'b', 'c' ])
), array_two (e) as (
select unnest(ARRAY[ 'c', 'd', 'e' ])
)
select array_agg(e order by e)
from (
(
select e from array_one
except
select e from array_two
)
union
(
select e from array_two
except
select e from array_one
)
) t;
你的逻辑是错误的。结果应该是(a,b,d,e)不仅(a,b)。 – 2013-02-25 13:11:06