(For people who have already read my answer: It has been hugely re-edited because of the comments of @nawfal that I want to preserve)
First I want to clarify that I understood your problem correct.
You have the Parts:
select id from table1
That produces Result1
select id from table2 where <some condition>
That produces Result2
select count(id) > 0 from table2 where <some condition>
That decides what result to use
If 3. returns more than 1 then you want the rows that are returned by 1. AND 2. (but not the ones that are only returned by 1. OR only by 2.)
If 3. returns 0 then you want the results of 1.
-> The solution is to have 3. in a view and select it in a where clause of a union statement TWICE.
Like this:
SELECT t1.id FROM table1 t1 WHERE
(SELECT COUNT(t2.id) from table2 t2 where <some condition>) = 0
UNION
select t2.id from table2 t2 WHERE <some condition>
AND t2.id IN (SELECT t1.id FROM table1 t1)
AND (SELECT COUNT(t2.id) from table2 t2 where <some condition>) > 0
one of the two parts will always be empty (because query 3. can't be = 0 and > 0 at the same time)