表1
名称  重量    
A      2
B      3
C      4
D      5表2
名称  重量
A      2
B      3从表1把表2名称相同的数据去掉,结果是
名称  重量
C      4
D      5
请问如何实现?谢谢
       

解决方案 »

  1.   

    select * from 表1
    except select * from 表2
      

  2.   

    use test
    go
    if object_id('test.dbo.表1') is not null drop table 表1
    -- 创建数据表
    create table 表1
    (
    名称 char(2),
    重量 int
    )
    go
    --插入测试数据
    insert into 表1 select 'A',2
    union all select 'B',3
    union all select 'C',4
    union all select 'D',5
    goif object_id('test.dbo.表2') is not null drop table 表2
    -- 创建数据表
    create table 表2
    (
    名称 char(2),
    重量 int
    )
    go
    --插入测试数据
    insert into 表2 select 'A',2
    union all select 'B',3
    go
    --代码实现
    --1#
    select * from 表1 except select * from 表2--2#
    select * from 表1 t where not exists(select 1 from 表2 tt where tt.名称=t.名称 and tt.重量=t.重量)/*测试结果
     
    名称    重量
    ---------------------
    C  4
    D  5(2 行受影响)
    */
      

  3.   

    你这是求两个表之间的差集。可以用操作符:not in;not exists;except 这些。其他的别人都写了,那我就写个not in 的吧。select * from 表1 where 名称+Convert(varchar(100),重量) not in
    (select 名称+Convert(varchar(100),重量) from 表2)注意的是except是sql2005的新功能,对sql2000不支持。
      

  4.   

    SELECT 表1.名称 ,表2.名称 
       FROM  表1 left join 表2
       on  表1.名称=表2.名称
    where 表2.名称 is null