问题
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