有两个表A、BA: id a1 a2 a3
1 1 1 1
2 2 2 2
3 3 3 3B: id b1 b2 b3
1 4 6 8
1 5 7 9
3 6 8 0
我想得到 
ida a1 a2 a3 idb b1 b2 b3
1 1 1 1 1 5 7 9
3 3 3 3 3 6 8 0该怎么作阿??
 

解决方案 »

  1.   

    简而言之就是我想得到的是A的一个id 对应的B的一个id的数据
      

  2.   

    我想得到 
    ida a1 a2 a3 idb b1 b2 b3 
    1 1 1 1 1 5 7 9 
    3 3 3 3 3 6 8 0 
    为啥没有1 4 6 8 ida a1 a2 a3 idb b1 b2 b3 
    1 1 1 1 1 5 7 9 1 4 6 8 
    3 3 3 3 3 6 8 0 
      

  3.   

    不是吧,2个表作inner join on A.id = B.id, 也会有3行啊(也就是有 1 4 6 8)如果只要你说的2行,用b join A, 就好了.我搞个测试给你看看
      

  4.   


    不对,b join A也会有3行
      

  5.   

    scott@ORA1> select * from a;        ID         A1         A2         A3
    ---------- ---------- ---------- ----------
             1          1          1          1
             2          2          2          2
             3          3          3          3scott@ORA1> select * from b;        ID         B1         B2         B3
    ---------- ---------- ---------- ----------
             1          4          6          8
             1          5          7          9
             3          6          8          0scott@ORA1> select * from a inner join b on a.id = b.id;        ID         A1         A2         A3         ID         B1         B2         B3
    ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
             1          1          1          1          1          4          6          8
             1          1          1          1          1          5          7          9
             3          3          3          3          3          6          8          0
      

  6.   

    select * from a join b on a.id = b.id;
    -- 或者
    select * from a,b where a.id=b.id;
      

  7.   

    select A.*, B.*
      from A,
           (select ID, max(B1) as B1, max(B2) as B2, max(B3) as B3
              from B
             group by ID) C
     where A.ID = B.ID
      

  8.   

    取id为1,值较大的那行
    SQL> select a.id ida, a.a1, a.a2, a.a3, t.id idb, t.b1, t.b2, t.b3
      2    from a,
      3         (select id, b1, b2, b3
      4            from (select b.*,
      5                         row_number() over(partition by id order by b1 desc, b2 desc, b3 desc) rn
      6                    from b)
      7           where rn = 1) t
      8   where a.id = t.id;
     
           IDA         A1         A2         A3        IDB         B1         B2         B3
    ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
             1          1          1          1          1          5          7          9
             3          3          3          3          3          6          8          0
     
    取值较小的那行
    SQL> select a.id ida, a.a1, a.a2, a.a3, t.id idb, t.b1, t.b2, t.b3
      2    from a,
      3         (select id, b1, b2, b3
      4            from (select b.*,
      5                         row_number() over(partition by id order by b1, b2 , b3 ) rn
      6                    from b)
      7           where rn = 1) t
      8   where a.id = t.id;
     
           IDA         A1         A2         A3        IDB         B1         B2         B3
    ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
             1          1          1          1          1          4          6          8
             3          3          3          3          3          6          8          0
      

  9.   


     select a.id,a.a1 ,a.a2 ,a.a3,
            b.id,sum(b.b1) as b1,sum(b.b2) as b2 ,sum(a.b3) as b3,
     from  a ,b 
     wher a.id= b.id
     group by b.id
     
      

  10.   


     select a.id,a.a1 ,a.a2 ,a.a3,
            b.id,sum(b.b1) as b1,sum(b.b2) as b2 ,sum(a.b3) as b3,
     from  a ,b 
     wher a.id= b.id
     group by b.id