select distinct b.工程名,a.编码
from b left join a on b.编码<>a.编码

解决方案 »

  1.   

    --删除测试环境
    create table a
    (
      [编码] int,[名称] varchar(20)
    )
    create table b
    (
      [工程名] varchar(20),[编码] int
    )insert a
    select 1,'a' union
    select 2,'b' union
    select 3,'c' union
    select 4,'d' union
    select 5,'e' 
    insert b
    select '工1',1 union
    select '工1',2 union
    select '工2',3 union
    select '工2',4--测试
    select distinct b.[工程名],a.[编码],a.[名称] 
    from b 
    left join a 
    on a.[编码] not in (select T.[编码] from b T where T.[工程名]=b.[工程名]) --删除测试环境
    drop table a
    drop table b--结果
    /*工程名                  编码          名称                   
    -------------------- ----------- -------------------- 
    工1                   3           c
    工1                   4           d
    工1                   5           e
    工2                   1           a
    工2                   2           b
    工2                   5           e
    */