A表
id     name
1      aa
2      bb
3      ccB表
name   aid
aa     1
cc     3
xx     4A表的ID=B表的aid,如何将B表在A表中无对应id的列读取出来,并插入A表?求教,谢谢。

解决方案 »

  1.   

    INSERT INTO A   SELECT * FROM B WHERE aid NOT IN(SELECT id FROM A)
      

  2.   

    insert into A(id,[name]) select id,[name] from B where id not in(select id from A)
      

  3.   

    insert into a 
    select aid,name from b where not exists(select 1 from a where a.id=b.aid)
      

  4.   


    declare @a table(id int, name varchar(20))
    declare @b table(aid int, name varchar(20))
    insert into @a select 1, 'aa' union all select 2, 'bb' union all select 3, 'cc'
    insert into @b select 1, 'aa' union all select 3, 'cc' union all select 4, 'xx'insert into @A(id, name)
    select aid, name from @b b where not exists(select 1 from @a a where a.id=b.aid)select * from @a
      

  5.   

    insert into a 
    select aid,name from b where not exists(select 1 from a where a.id=b.aid)
      

  6.   

    insert into
     a 
    select
     aid,name 
    from
     b 
    where
     not exists(select 1 from a where a.id=b.aid)