I’m building a complex search application with a large number of potential inputs. Each input is optional. I’m trying to build a query in a modular fashion.
Let’s say I have two distinct queries, each with independent WHERE conditions:
SELECT * FROM t1 WHERE t1.c1 = x
SELECT * FROM t2 WHERE t2.c1 = y
I’ve found the following code to be valid:
SELECT * FROM t1 INNER JOIN t2 ON t1.c2 = t2.c2 WHERE t1.c1= x AND t2.c1= y
However, since the WHERE conditions are grouped together at the end, it would be very difficult to implement this in a modular fashion. So, I tried this:
SELECT * FROM t1 WHERE t1.c1 = x INNER JOIN ON t1.c2 = t2.c2 WHERE t2.c1 = y
However, this code is invalid. What’s the best way to join an arbitrary number of tables with independent WHERE clauses?
The joins between the tables may be able to be implemented conditionally — and the
whereclauses concatenated at the end — like so:And you can build this as a string conditionally depending on which options the user chooses.
While building the
whereclauses and thejoinstogether dynamically may work, there may be some pitfalls to watch out for.First, the joins will still likely have to be custom built based on the the natural way the tables join together — for example,
t3may only be joinable tot2and nott1. You’ll have similar issues with thewhereclauses. Each where clause will have to be custom built depending on the columns in the table.In other words, it’s unlikely that this problem is completely generalizable. In the end you’ll need to craft custom SQL that will depend on the specifics of the tables. Worse yet, over time the tables will change and the SQL will have to change with it. If you are building complicated SQL dynamically, you may be setting yourself up for a maintenance headache.
Moreover, as your app grows you’ll likely want to tune the SQL for each combination to optimize the queries. Make sure you try to take this into account as you build this out.