Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using two fields with "in" operator in QueryDSL

I have to write this query using QueryDSL:

select *
from table
where(field1, field2) in (
    select inner_field_1, inner_field2
    from ...
);

However, I don't know how to use two fields (field1 and field2) with an "in" operator in QueryDSL. I have been looking for it in the documentation but I haven't seen any example of two fields.

This is what I have so far:

Expression<?>[] projection = {
    table.field1,
    table.field2
};

SQLSubQuery outterQuery= new SQLSubQuery()
    .from(table)
    .where([some expression].in(inneryQuery.list(projection))) // ???
    .groupBy(contentcache1.programId, contentcache1.id);

Any help would be appreciated

Thank you very much in advance

like image 363
Genzotto Avatar asked Apr 21 '26 10:04

Genzotto


2 Answers

You can express it via

SQLSubQuery outerQuery = new SQLSubQuery()
    .from(table)
    .where(Expressions.list(column1, column2, ...).in(inneryQuery.list(projection))) 
    .groupBy(contentcache1.programId, contentcache1.id);
like image 128
Timo Westkämper Avatar answered Apr 22 '26 22:04

Timo Westkämper


You can rewrite your original query as:

select *
from table, (select distinct inner_field_1, inner_field2 from ...) subquery
where field1 = subquery.field1 and field2 = subquery.field2

Then you don't have to use the IN operator.

like image 33
Rémi Avatar answered Apr 23 '26 00:04

Rémi



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!