Oracle数据库描述:有表table,table有2个字段A和B,主键是(A,B),我想把A=‘a1’的所有B数据插入A=‘a2’中,SQL语句该怎么写?

解决方案 »

  1.   

    比如说select B from table where A='a1'得出的结果再次插入table表中,但A='a2'。
    如select B from table where A='a1'得出的结果是:
    B
    b1
    b2
    b3
    b4
    b5
    ...
    然后
    insert into table (A,B)values(‘a2’,‘b1’);
    insert into table (A,B)values(‘a2’,‘b2’);
    insert into table (A,B)values(‘a2’,‘b3’);
    insert into table (A,B)values(‘a2’,‘b4’);
    ...
      

  2.   

    insert into table
    (A,B)
    select 'a2',B from table where A='a1'
      

  3.   

    谢谢HelloWorld_001 !!!!
      

  4.   

    如果表的字段很多,insert都要列出很多字段,有没有更好的方法不用列出所有的字段,我只是想加记录的时候改变A的值而已。
      

  5.   

    insert into table
    --(A,B,...)   --所有字段,可以不见但不提倡这样写
    select 'a2',B,... from table where A='a1'  --所有字段,都要写多写点字段不麻烦吧!