如果在另一个表中找到记录,请从表中选择

Noa*_*tin 3 mysql sql oracle oracle11g

我需要从表1中选择一些行,如果在表2中找到一个值,那么我想检查表2中是否找到值(我将从命令行输入值),然后从Table1中选择行,如果不是我想从另一个表中选择行.我试过CASE但是从我得到的只有当你想在一个表中检查值时才有效.任何的想法?

Dmi*_*nko 6

你可以这样做:

-- 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)
Run Code Online (Sandbox Code Playgroud)