SelectQueryBuilder - Query multiple columns with same value

665 views Asked by At

When using SelectQueryBuilder I'm struggling to get the desired query to build.

I have a search window that takes multiple values and builds the relevant search string for the database. That all works fine, the problem comes when looking at 2 specific fields.

The database records holds multiple email addresses for each contact, however I can't seem to write the code needed to build the string that looks at each email column and compares the search term.

The desired result is a query that looks like

SELECT col1, col2, col3 
FROM table 
WHERE (
          (email = val OR email2 = val) 
          AND col4 = val2 
          AND col5 = val3
      )

and im trying to avoid a situation that looks like

SELECT col1, col2, col3 
FROM table
WHERE 
    (email = val AND col4 = val2 AND col5 = val3) 
OR 
    (email2 = val AND col4 = val2 AND col5 = val3)

Is this possible using SelectQueryBuilder or should I look at alternative methods?

Note: I can achieve the second result, but it is incredibly messy and would be difficult to maintain should additional fields be required in the future

0

There are 0 answers