日期:2014-05-17  浏览次数:20900 次

oracle表关联查询
  
数据库中表如图,其中第二张表中的bank_id brand_id,check_flag均对应第一张表中的value,现在想通过id查出其对应的value_description,但是第二张表某些列的值value中并不存在,例如bank_id有一条数据为100,但是第一张表中没有value就没有100,但这条数据的brand_id在value中存在,现在想只要其中一个字段存在就把这条数据查出来,请教大大神们应该如何写SQL语句,谢谢
------解决方案--------------------
左边的表记为a ,右边为b
select value_description from a 
where exisit (select 1 from (select distinct bank_id id from b
                            union 
                            select distinct brand_id id from b
                            union 
                            select distinct check_flag id from b) c 
             where a.value=c.id)
------解决方案--------------------
左边的表记为a ,右边为b,check_flag 是字符型吧,要转换下
select value_description from a 
where exisit (select 1 from (select distinct bank_id id from b
                            union 
                            select distinct brand_id id from b
                            union 
                            select distinct to_number(check_flag) id from b) c 
             where a.value=c.id) 
------解决方案--------------------
select distinct 
       a1value as '银行名字',
       a2.value as '品牌’    
       a3.value as  '对账'
from b,
     a a1,
     a a2,
     a a3
where b.brank_id=a1.value(+)
  and b.brand_id=a2.value(+)
  and b.check_flag=a3.value(+)