两个表A,B。根据某一条件取记录,两表中有且只有一条记录,这样的sql语句怎么写。只用一句

解决方案 »

  1.   

    select * from a where column1 = condition1
    union
    select * from b where column1 = condition1;
      

  2.   

    select XXXX,XXXX from 表A
    where ....
    union
    select XXXX,XXXX from 表B
    where ....
      

  3.   

    select cnt from (
      select count(1) as cnt from a
      where condition......
      union all
      select count(1) as cnt from b
      where condition......
       and 0=(select nvl(count(1),0) from a
      where condition...... )
      ) t
    where cnt=1;
      

  4.   

    说得不太明白,首先A,B两个表需要有关联字段,假设A,B 两个表通过字段ID 进行关联
    那么select * from A a,B b where a.ID=b.ID and 某一条件 
      

  5.   

    查询时涉及两个表A 和B , 都有字段f,从两个表中查找字段f=‘a’的记录,两个表总共能找到有且只有一条记录。
      

  6.   

    比如A 
    seq A1,A2
    101 a  b
    105 a  c
    B
    seq B1,B2
    103 b  b
    104 b  c
    查找seq为某一值的记录,A,B中有且只有一条记录 例如查找seq=103的
              seq  f1 f2
    结果就是 103   b  b
      

  7.   

    select seq,A1,A2 count(1)  from 
    (select seq ,A1,A2 from a where column1 
    union all 
    select seq , B1,B2 from b where column1)
    group by seq,A1,A2  having  count(1) = 1 ;
      

  8.   


    --首先A,B表结构的字段要一样
    select *
    from 
    (select seq,A1 f1,A2 f2 from A
    union all
    select seq,B1 f1,B2 f2 from B) t
    where seq=103
      

  9.   

    主的描述估计一般人理解不出来!
    select * from 
    ((select * from a
    minus
    select * from b)
    union all
    (select * from b
    minus
    select * from a)) x
    where x.seq=??
      

  10.   

    不好意思。结果集还得有个字段 source,如果取自A,对应 是‘A’,如果取自B,则为‘B’
    比如A   
    seq A1,A2
    101 a b
    105 a c
    B
    seq B1,B2
    103 b b
    104 b c
    查找seq为某一值的记录,A,B中有且只有一条记录 例如查找seq=103的
      seq f1 f2 source
    结果就是 103 b b B
    这些要用一句sql实现,怎么写啊。多谢
      

  11.   

    提问题尽量表达清楚,给出条件,然后想要的结果。 最好给一定数据更清楚。
    select *
      from (
            select seq, A1 f1, A2 f2, 'A' source  from A
            union all
            select seq, B1 f1, B2 f2, 'B' source from B
            ) t
     where seq = 103