语句如下:
insert into abc
select a.id, b.name, c.dept 
    from a, b, c
    where b.id=a.id and b.type=1
          and c.id=a.id其中在a、c、abc三个表中id都是主键,但在表b中主键是id+type+name,就是说以id和type为条件会查得多条结果,但我只需要从中任意一条记录即可插入abc表,因为abc表中id是主键。我试着用left join来关联b表,好像还是不行,请大家指点。谢谢。

解决方案 »

  1.   

    用rownum试试insert into abc
    select a.id, b.name, c.dept 
        from a, b, c
        where b.id=a.id and b.type=1
              and c.id=a.id
              and rownum=1;
      

  2.   

    to njhart2003:
    谢谢回复,但你这样一共只会向abc表插入一条记录,而我的意思是对应每个id 插入一条记录.
      

  3.   

    樓主的意思應該要求同時能插入多條記錄
    insert into abc
    select a.id, b.name, c.dept 
        from a, b, c
        where b.id=a.id and b.type=1
              and c.id=a.id 
     and b.name in
    (select name from b where rownum = 1)
      

  4.   

    不好意思,還需要條件:
    樓主的意思應該要求同時能插入多條記錄
    insert into abc
    select a.id, b.name, c.dept 
        from a, b, c
        where b.id=a.id and b.type=1
              and c.id=a.id 
     and b.name in
    (select name from b d where rownum = 1 and id = b.id and type = b.type)
      

  5.   

    to  tommysun:
    谢谢回复,你是明白我的意思了:)方法也可行,但是如果对于a表中的一些id,如果在b表或c表中没有对应的记录,就不会向abc表插入记录,而我希望此时b.name, c.dept 以空值代替。这样最后插入到abc表中的记录总数应该和a表记录数一样多。谢谢,请多指教!
      

  6.   

    insert into abc
    select a.id, b.name, c.dept 
        from a, b, c
        where b.id(+)=a.id and b.type=1
              and c.id(+)=a.id 
     and (b.name in
    (select name from b d where rownum = 1 and id = b.id and type = b.type) or b.name is null)