我需要从表1中选择一些行,说在表2中是否找到一个值。因此,我想检查是否在表2中找到了该值(我将从命令行输入该值),然后从表1中选择行,如果不是,我想从另一个表中选择行。我尝试了CASE,但是只有当您想检查一个表中的值时,我才能从CASE中获得结果。任何的想法?
您可以执行以下操作:
-- 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)