Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use 'ANY' function with JOOQ

I currently use JOOQ to get data from postgresql DB. Then I have faced a problem to get data which data-type is array type.

Current condition is like below:

Table schema is:

CREATE TABLE favorites (
    id int,
    items    varchar(100)[]
);

Sample data is:

INSERT INTO favorites (id, items)
    VALUES (1, '{orange, lemon, banana}');
INSERT INTO favorites (id, items)
    VALUES (2, '{apple, grape}');

To get first data, SQL is like:

SELECT id, items FROM favorites WHERE 'orange' = ANY (items);

But, I cannot create sql like above with JOOQ.

Connection connection = ...;
DSLContext context = DSL.using(connection, ...);
List<Table> table = context.select().from(TABLE).fetchInto(class.TABLE);

Can I use ANY function with JOOQ? If it is possible, how can I create it with JOOQ. If not, is there any other ways to get the same result with JOOQ?

Thank you in advance.

like image 511
tsuxkky Avatar asked Sep 12 '25 05:09

tsuxkky


1 Answers

Do it like this:

List<TableRecord> table = context
    .selectFrom(TABLE)
    .where(val("orange").eq(any(TABLE.ITEMS)))
    .fetch();

The above query uses DSL.any(Field<T[]>)

like image 172
Lukas Eder Avatar answered Sep 13 '25 18:09

Lukas Eder