有三张表A、B、C,其中B、C的外键'aid'是A的主键。但是A中的记录不会同时出现在B、C中。现有两条语句:
select b_val from B where aid in (select id from A where ...);
select c_val from C where aid in (select id from A where ...);
请高手指点一下我该怎么把这两句合并成一句,谢谢!!!Oracleselect合并

解决方案 »

  1.   

    select * from b left join c on b.aid=c.aid where b.aid in (select id from A where ...) 
      

  2.   

    但是A中的记录不会同时出现在B、C中,所以这条语句只能查出select b_val from B where aid in (select id from A where ...);
    对应的记录
      

  3.   

    你现在的目的是找出A中的记录同时出现在B、C中的有哪些记录?select b_val from B where aid in (select id from A where ...);
    select c_val from C where aid in (select id from A where ...);
    请高手指点一下我该怎么把这两句合并成一句,谢谢!!!
      

  4.   

    (select id from A where ...)对应的id值要么等于B中的aid,要么等于C中的aid
      

  5.   

    select id from a where id in (select aid from b) or id in (select cid from c)--即A中的ID要么在B中,要么在C中
      

  6.   

    select b_val
      from (select b_val, aid
              from B
            union all
            select c_val, aid from c) t
     where t.aid in (select id from A where .. .)
      

  7.   

    我需要的最终结果大概是这样的:
    SELECT c_val||b_val as val FROM ....