两张表,结构相同,有两个字段,且字段1 + 字段2是主键,表1的数据是表2的子集,假设表1中有些数据表2中没有,如何删除这些数据?

解决方案 »

  1.   

    delete 表1  where not exsits (select *  from  表2)
      

  2.   

    create table #t1
    (
    col1 int,
    col2 int,
    col3 int
    )create table #t2
    (
    col1 int,
    col2 int,
    col3 int
    )insert into #t1 select 1,1,1
    insert into #t1 select 2,2,2
    insert into #t1 select 3,3,3insert into #t2 select 1,1,1
    insert into #t2 select 2,2,2
    insert into #t2 select 4,4,4select * from #t1
    select * from #t2delete t
    from #t1 t
    where not exists(select null from #t2 where t.col1= col1 and t.col2=col2)
      

  3.   

    select * from #t1col1        col2        col3
    ----------- ----------- -----------
    1           1           1
    2           2           2(2 row(s) affected)
      

  4.   

    delete from table1 where not exists(select * from table2 where table1.字段1=table2.字段1 and table1.字段2=table2.字段2)