AND t1.Team IN (SELECT Team FROM t2 WHERE t3.ID = t2.ID)
How can I make above AND as optional, if the sub-query does not have any results, do not even consider AND t1.Team IN ("")
.
Something like this does not work:
AND IF((SELECT Team FROM t2 WHERE t3.ID = t2.ID), (t1.Team IN (SELECT Team FROM t2 WHERE t3.ID = t2.ID)), 1)
4
Answers
You can use Case Statement for check if else condition inside the SQL query syntax.
For that I think your query looks like below:
Formally you need in
But moving this subquery to FROM clause with proper left-joining seems to be more reasonable in your case.
Might be bit faster to do (if t1.Team is not NULL):
Here’s a nice trick to keep the query simple and good performance:
explanation:
this sub-query does the following:
This means 1 will be returned from sub-query in 2 cases: