需要从另一个表导入数据,如果主键相同则更新该纪录,不存在则插入新纪录,谢谢指教

解决方案 »

  1.   

    首先删除相同的记录
    然后把所有的记录插入旧表insert into oldTable select * from newTable 或者先插入 不相同的记录
    insert into oldTable select * from newTable where newTable.id not in(select id from oldTable)
      

  2.   

    UPDATE 表1 set 字段 from 表1,表2 where 表1.主键=表2.主键insert into 表1 (....) select ....from 表2 where 表2.主键 not in (select 主键 from 表1)
      

  3.   

    if判断啊
    if 如果存在 
    updata
    else
    insert
      

  4.   

    表1
    主键,字段1,字段2,字段3,……表2
    主键,字段1,字段2,字段3,……
    //首先更新
    update (select 表1.主键,表1.字段1,表1.字段2,表2.字段1,表2.字段2 from 表1,表2 where 表1.主键=表2.主键))
       set 表1.字段1=表2.字段1, 表1.字段2=表2.字段2;
    //插入就简单了
    insert .....
      

  5.   

    create table t1
    (
    FID int primary key,
    Name varchar(10)
    )insert into t1 values(1,'aaa')insert into t1 values(2,'bbb')create table t2
    (
    FID int primary key,
    Name varchar(10),
    des varchar(50),
    )insert into t2 values(1,'ccc','sdf')
    请问如何把t1表的内容导入到t2,不能先删除t2的内容
    导入后t2的表内容为
    1,'aaa','sdf'
    2,'bbb',''
    谢谢了
      

  6.   

    -- update
    update t2 set name = (select name from t1 where t1.fid = t2.fid)
    where exists (select 1 from t1 where t1.fid = t2.fid);-- insert
    insert t2 (fid, name, des)
    select fid, name, '' from t1 where not exists (select 1 from t2 where t2.fid = t1.fid);