表A
peono peoname 
0001  张三
0002  李四
0003  王五
表B
peono xiaofei   caozuo  yu'e            type
0001      2.5 .0000 276.04 消费
0001      2.5 .0000 273.54 消费
0001      2.5 .0000 271.04 消费
0001      2.5 .0000 268.54 消费
0001      3.2 .0000 265.34 消费
0001      3.2 .0000 262.14 消费
0001      3.2 .0000 258.94 消费
0001      3.2 .0000 255.74 消费
0001      3.2 .0000 252.54 消费
0001      3.2 .0000 249.34 消费
0001      .00 150.0 399.34 补贴
0001      2.0 .0000 397.34 消费
0002      2.0 .0000 100.00 消费
0002      2.0 .0000 98.000 消费
0002      2.0 .0000 96.000 消费
0002      2.0 .0000 94.000 消费
0002      3.0 .0000 91.000 消费
0002      3.0 .0000 88.000 消费
0002      3.0 .0000 85.000 消费
0002      3.0 .0000 82.000 消费
0002      3.0 .0000 79.000 消费
0002      3.0 .0000 76.000 消费
0002      2.0 .0000 74.000 消费
0003      2.0 .0000 85.000 消费
0003      3.0 .0000 82.000 消费
0003      3.0 .0000 79.000 消费
0003      3.0 .0000 76.000 消费
0003      3.0 .0000 73.000 消费
0003      3.0 .0000 70.000 消费
0003      3.0 .0000 67.000 消费
0003      .00 100.0 167.00 补贴
0003      2.0 .0000 165.00 消费
用SQL语句结合表A与表B查询出没有补贴类别的员工:
0002  李四

解决方案 »

  1.   

    declare @t1 table(peono varchar(10),name varchar(10))
    declare @t2 table(peono varchar(10),type varchar(10))
    insert @t1 select '0001',  '张三'
    union all select '0002',  '李四'
    union all select '0003',  '王五'
    insert @t2 select '0001','消费'
    union all select '0001','补贴'
    union all select '0002','消费'
    union all select '0003','消费'
    union all select '0003','补贴'SELECT distinct a.* from @t1 a 
    left join @t2 b on a.peono = b.peono and b.type = '补贴' 
    where b.peono is null
      

  2.   

    SELECT  *
    FROM    表A
    WHERE   peono NOT IN
            (SELECT  peono
             FROM    表B
             WHERE   type = '补贴'
             )
    GO
      

  3.   

    insert @t1 select '0001',  '张三'
    union all select '0002',  '李四'
    union all select '0003',  '王五'
    insert @t2 select '0001','消费'
    union all select '0001','补贴'
    union all select '0002','消费'
    union all select '0003','消费'
    union all select '0003','补贴'
    这样只针对三个人,要是上千人也要一个个建立吗?