First let me say that mjfgates may be correct. The original psuedo code is not "bad" just because it takes two steps. The more complex your SQL statement, the more likely it is that the query engine will not be able to find the optimal plan. In this particular case, this is less likely because there is only one table that we refer to several times, but this is something that needs to be remembered in general in these situations. Getting SQL to one statement is not always a worthy goal.
Now, to answer your question:
select colA,colB,colc from table1 t1 where ( (col1 = $var1 and col2 like '%$var2%') and EXISTS (select 1 from table1 t2 where t2.col1 = $var1) ) or ( (col2 LIKE %$var1%) and NOT EXISTS (select 1 from table1 t3 where t3.col1 = $var1) )
source share