Search code examples
jpaspring-data-jpaquerydsl

Querydsl case when in where clause


Am looking for example on use case when condition in where clause on querydsl.

SELECT column1, column2 FROM viewWhatever WHERE CASE WHEN column1 == 'b' THEN account_location = ? WHEN column1 =='m' THEN location_area = ?

I just recently start using querydsl I can't figure out how to represent above query in querydsl case builder expression. I found examples for use in select clause but couldn't find much for use in where clause.


Solution

  • Do you really need a CASE statement?

    SELECT 
       column1, column2 
    FROM 
       viewWhatever 
    WHERE 
       (column1 == 'b' AND account_location = ?)
    OR
       (column1 =='m' AND location_area = ?)
    

    Can obviously be easily represented in QueryDSL.