有两个表A(字段1,2,3),B(字段4,5,6),其中A表primary key(1,2),B表primary key(4)
现在把A表中的字段2,3取出分别赋给B表的4,5字段。其中,A表中字段2重复的只取一条。就这样,我想不出怎么处理,当然,用存储过程来处理是能够处理的。感觉总能用select语句检索,再用insert插入的复合语句完成,哪位大哥知道,谢谢。
用group by就能保证检索出来的A表2字段的内容唯一。insert into B(4,5) 
       select 2,3 from A group by 2;这样语句是错的,group by 语句错误。select 2 from A group by 2 正确,但是
select 2,3 from A group by 2就是错的,为什么?

解决方案 »

  1.   

    insert into B(4,5) 
    select 2,3 from A a,(select 2,min(rowid) min_rowid from A group by 2) b 
    where a.rowid=b.min_rowid;
      

  2.   

    漏了一个“a.”insert into B(4,5) 
    select a.2,3 from A a,(select 2,min(rowid) min_rowid from A group by 2) b 
    where a.rowid=b.min_rowid;
      

  3.   

    2重复只取1条,去最大或最小的3 就可以了
    insert into B(4,5) 
           select 2,max(3) from A group by 2;