PostgreSQL Select WHERE column = [Array]

戏子无情 提交于 2021-01-29 09:01:24

问题


I have a table1 with column 1 and 2

1        2
Banana   x
Apple    y
Orange   z

I want to call a SELECT on this table with an array as parameter: ['Banana', 'Apple']

I want to get the rows where column 1 contains Banana or Apple (elements of the array)

SELECT * FROM table1 WHERE column1 = 'Banana OR column1 = 'Apple'

But how is this working dynamically? Like where column1 = elemt in array....?


回答1:


Use the ANY operator:

select *
from the_table
where the_column = any( array['Banana', 'Apple'] );



回答2:


Use the keyword IN:

SELECT * FROM table1 WHERE column1 IN ('Banana', 'Apple');

(reference doc.: https://www.postgresql.org/docs/current/functions-subquery.html#AEN16806)



来源:https://stackoverflow.com/questions/60656624/postgresql-select-where-column-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!