如何检查单行中的任何数组值是否在我的列表中? 这是我的桌子。我们称之为ABC
id | page_id | values
------------------+-------------+-------------------------------------------------------------------------
1376092679147519 | xyz | {6004036173148,6003373173651,6003050657850}
1375487155874738 | xyz | {6003301698460,6003232518610}
1497527026945449 | xyz | {6003654559478,6003197656807}
1375388575884596 | xyz | {6003512053894,6003450241842,6003051414416}
1319144441504401 | xyz | {6004001256506,6003514818642,6003400993421}
我的目标是选择那些行,其中一个值出现在给定列表中(' 6004036173148',#39; 6003197656807')。
SELECT id, page_id, values from ABC WHERE -SOME CLAUSE- IN ('6004036173148', '6003197656807');
id | page_id | values
------------------+-------------+-------------------------------------------------------------------------
1376092679147519 | xyz | {6004036173148,6003373173651,6003050657850}
1497527026945449 | xyz | {6003654559478,6003197656807}
这是我的PosgreSQL表的结构
Table "public.ABC"
Column | Type | Modifiers
--------------------+--------------------------+------------------------
id | character varying | not null
page_id | character varying | not null
values | character varying[] |
Indexes:
"ABC_pkey" PRIMARY KEY, btree (id)
答案 0 :(得分:2)
如果我理解正确,你需要这个:
select * from t
where "values" && '{6004036173148,6003197656807}'::character varying[]
修改强>
如果您需要提取某些值,则可以使用unnest
函数
注意,如果同一个数组包含来自搜索列表的多个值,则会重复行。用id=2
查看此示例输出,您可以看到我正在谈论的内容:
with t(id, values) as(
select 1, '{6004036173147,6003373173651,6003050657840}'::character varying[]
union all
select 2, '{6004036173148,6003373173652,6003050657850}'::character varying[]
union all
select 3, '{6004036173149,6003373173653,6003050657860}'::character varying[]
)
select tt.* from
(select t.*, unnest(values) unn from t) tt
inner join (select unnest('{6003373173651,6004036173148,6003373173652}'::character varying[]) v ) lst
on tt.unn = lst.v