[英]Postgres check if any of array values is in a list
How can I check if any of an array values in a single row is in my list? 如何检查列表中是否有单行中的任何数组值?
Here is my table. 这是我的桌子。 Let's call it ABC 我们称之为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}
My aim is to select those rows, where one of the values appears in the given list ('6004036173148', '6003197656807'). 我的目的是选择那些行,其中一个值出现在给定列表中(“ 6004036173148”,“ 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}
Here is the structure of my PosgreSQL table 这是我的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)
If I correct understood, you need this: 如果我正确理解,则需要这样做:
select * from t
where "values" && '{6004036173148,6003197656807}'::character varying[]
EDIT 编辑
If you need extract certain values, then you can use unnest
function 如果您需要提取某些值,则可以使用unnest
函数
Note, that if same array contains more than 1 value from searched list, then row will be repeated. 请注意,如果同一数组包含来自搜索列表的多个值,则将重复行。 Look this example output with id=2
and you can see, about what I'm talking: 看一下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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.