Home > Mobile >  If select returns 0 then do another select
If select returns 0 then do another select

Time:10-31

I have this query that selects data from two different connections

select count (*) from table1
where condition1 = 'answer1'
and time >= sysdate - interval '5' minute

UNION ALL

select count (*) from table2
where condition2 = 'answer2'
AND NOT EXISTS (

select * from table1
where condition = 'answer'
and time >= sysdate - interval '5' minute

);

My problem is that the table2 select doesn't work at all even though there is no data in table1.

Can anybody help me to get this to work, or knows another way to write the query.

The idea is that if select count returns 0 then do another select from different table

Tried googling my way around even lurking here but still didn't get any answers

CodePudding user response:

If table2 is not connected to table1 someway within the NOT EXISTS clause it will never return data because so long as the query returns something it exists.

If you don't have a way of linking table2 to table1 that you can use as a join within the not exists you can do something like this

select count (*) from table 1
where condition = answer
and time >= sysdate - interval '5' minute

UNION ALL

select count (*) from table2
where condition2 = answer2
AND (
  select count(*) from table 1
  where condition = answer
  and time >= sysdate - interval '5' minute
) = 0;
  • Related