I need to select some rows from Table 1 lets say if a value is found in Table 2. So I want to check if the value (I will enter the value from command line) is found in Table
This query will select from Table1
if :id
exists in Table3
, and from Table2
otherwise:
select *
from Table1
where exists
(
select *
from Table3
where id = :id
)
union all
select *
from Table2
where not exists
(
select *
from Table3
where id = :id
)
You can do something like this:
-- If value is found in table2, select from table1
select * -- <- use padding if necessary
from table1
where exists (select 1
from table2
where myField = value)
union all
-- If value is not found in table2, select from another_Table
select * -- <- use padding if necessary
from another_Table
where not exists (select 1
from table2
where myField = value)