如何用sql查询呢有两个表记录分别如下A表
a   ia
-----
a   1
b   2
c   3
c   4
B表
b   ib
-----
b   1
c   2
c   3
c   4
d   5我想要如下 将a和b表连接  的查询结果:
a   b  ia  ib
-----------------
a      a1
b   b  a2  b1
c   c  a3  b2
c   c  a4  b3
    c      b4
    d      b5
如果使用full outer join连接后则结果不同,将会出现2x3=6条值为c的记录,而我只想简单匹配就行,用一个少一个,两表连接后, 表示的是真实记录结果,请高人指点

解决方案 »

  1.   

    如果使用full outer join连接后则结果不同,将会出现2x3=6条值为c的记录
    ==============================================================
    因为你只有a.a=b.b一个条件,
    而看你的需求还要有一个条件,但是你没有説明
    就数据看,加一个a.ia=b.ib条件,full outer join可以的,
      

  2.   

    select a, b,  NVL2(ia,'a','')||ia ia, NVL2(ib,'b', '')||ib ib
      from a full outer join b
       on(a.a=b.b and a.ia=b.ib+1)
     order by a,b;
      

  3.   

    先回复2、3楼两位
    ia和ib两字段不用理会,表示本表其他字段,a表a字段 和 b表b字段,我想让两表根据字段值判读将两表连接,即a.a=b.a
    用full outer join 连接后的表中看a列和b列 则都会有2x3个c值,我想在一个连接后的表中可以看出a表或b表有几条c值的记录,并且是否可以跟另个表的值匹配得上
      

  4.   

    先回复2、3楼两位
    ia和ib两字段不用理会,表示本表其他字段,a表a字段 和 b表b字段,我想让两表根据字段值判读将两表连接,即a.a=b.a
    用full outer join 连接后的表中看a列和b列 则都会有2x3个c值,我想在一个连接后的表中可以看出a表或b表有几条c值的记录,并且是否可以跟另个表的值匹配得上
      

  5.   

    不好意思结果写的有点出入,呵呵ia和ib是什么值不用关心他,只描述其他字段而已我想要如下 将a和b表连接  的查询结果: 
    a  b  ia  ib 
    ----------------- 
    a      1 
    b  b   2   1 
    c  c   3   2 
    c  c   4   3 
       c       4 
       d       5 
      

  6.   

    drop table a;
    drop table b;
    create table a (a char(1), ia int);
    create table b (b char(1), ib int);insert into a values('a', 1); 
    insert into a values('b', 2); 
    insert into a values('c', 3); 
    insert into a values('c', 4);  insert into b values('b',1); 
    insert into b values('c',2); 
    insert into b values('c',3); 
    insert into b values('c',4); 
    insert into b values('d',5); select a, b, ia, ib
    from (
    select a, ia, row_number() over(partition by a order by ia) rn
    from a) t1
    full outer join (
    select b, ib, row_number() over(partition by b order by ib) rn
    from b) t2 on t1.rn = t2.rn and t1.a = t2.b
    order by a, b;A B         IA         IB
    - - ---------- ----------
    a            1
    b b          2          1
    c c          3          2
    c c          4          3
      c                     4
      d                     5
      

  7.   

    select a, b, ia, ib
      from A
      full outer join B on A.ia = (B.ib + (select (ia - ib) as i
                                             from A, B
                                            where A.a = B.b
                                              and rownum = 1))如此便可,不应该盲目地加  1,1应该是变量的。
      

  8.   

    分析后7楼应该是正确,不过查询结果还有问题,是不是属于full outer join复杂查询引起的问题,修改如为如下后结果正确,感谢大家,分数就发给7楼兄弟了。
    With a1 As (select a, ia, row_number() over(partition by a order by a) arn from a),
         b1 As (select b, ib, row_number() over(partition by b order by b) brn from b)
    select a,b,ia,ib,arn,brn From a1 
    full outer join b1 On a1.a=b1.b And a1.arn=b1.brn  order by a, b;
      

  9.   

    8楼的语句我没怎么分析,直接查询了结果还是不对,c d 对应错了,嘿嘿
    1 a 1
    2 b b 2 1
    3 c c 3 2
    4 c d 4 3
    5 c 4
    6 c 5
    7 c 6
      

  10.   


    select a, b, ia, ib
      from A
      full outer join B on A.ia = (B.ib + (select (ia - ib) as i
                                             from A, B
                                            where A.a = B.b
                                              and rownum = 1)) order by ia,ib那就ORDER BY一下吧,在我机器上是正确的,,,
      

  11.   

    呜呜呜~~~~回了这么多贴,现在还是一分不得,,,哈哈~
    我不就是忘了个ORDER BY 吗?大哭