這裏是另一種選擇:
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