Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Postgresql what to pass in WHERE clause for enum type of column to select all types?

I have a table column in Postgresql which has a data type of enum type. Now I am using a query like below to select data from the table.

SELECT ................. FROM ............ WHERE transaction = ?::operations.transactions;

I am passing this query to the PreparedStatement object of JAVA. It works fine when I pass any of the enum type as parameter in setString method of the PreparedStatement.

Now I want to ask that is it possible to send a string as parameter to setString method of the PreparedStatement so that I can get all the enum types in the ResultSet?

If it is not possible with this statement, how else could I change the query to get the desired result? Or any other strategy that I could use to get the desired result?

like image 670
Blip Avatar asked Jan 25 '26 05:01

Blip


1 Answers

You can use any(array expression). Example:

create type color as enum('white', 'blue', 'green');

with the_data(a_color) as (
    values ('white'::color), ('blue'), ('green')
)

select *
from the_data
where a_color = any(array['white', 'blue']::color[]);
like image 180
klin Avatar answered Jan 26 '26 22:01

klin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!