0

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

Tab Alleman
  • 31,483
  • 7
  • 36
  • 52
Takarii
  • 1,612
  • 2
  • 18
  • 29
  • I dont see whats wrong with your first sql code – BugFinder Apr 10 '17 at 11:03
  • @BugFinder There is nothing wrong with either sql code. The problem is that i cant figure out how to achieve the first one using select query builder. I can achieve the second one, but it is incredibly messy and hard to maintain – Takarii Apr 10 '17 at 11:05
  • "or should I look at alternative methods" - I would look for any ORM, like Entity Framework, just because it will be much more maintainable and clear that raw sql queries, even with query builder. – Evk Apr 10 '17 at 11:11
  • @Evk unfortunately I dont have time to rip out the old system and replace it with EF right now (its on the list of things to do once this current application is live) – Takarii Apr 11 '17 at 14:14

0 Answers0