选择Postgres中所有数组中存在的值

时间:2013-08-29 10:52:09

标签: sql postgresql

我有一些表ignore,其中col ignored_entry_ids包含整数数组。例如:

id     ignored_entry_ids
1      {1,4,6}
2      {6,8,11}
3      {5,6,7}

如何选择阵列中每行中存在的数字? (考试中有6个)

1 个答案:

答案 0 :(得分:3)

如果你的数字在数组中是唯一的,你可以做这样的事情,不要认为没有unnest

with cte as (
    select id, unnest(ignored_entry_ids) as arr
    from ign
)
select arr
from cte
group by arr
having count(*) = (select count(*) from ign)

sql fiddle demo

如果数字不唯一,请添加distinct

with cte as (
    select distinct id, unnest(ignored_entry_ids) as arr
    from ign
)
select arr
from cte
group by arr
having count(*) = (select count(*) from ign)