我需要从表 1 中选择一些行,假设是否在表 2 中找到值。所以我想检查是否在表 2 中找到值(我将从命令行输入值),然后从表 1 中选择行,如果不是,我想从另一个表中选择行。我尝试了 CASE,但从我得到的结果来看,只有当你想检查一个表中的值时它才有效。任何想法?
			
			6165 次
		
2 回答
            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)
于 2013-08-19T09:54:01.653   回答
    
    
            1        
        
		
Table1如果:id存在于 中Table3,此查询将从中选择,Table2否则从中选择:
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
        )
于 2013-08-19T09:52:46.930   回答