select a.id ,a.pid,a.cid ,[istb]=(case exists(select * from a,b where a.id=b.id and 
a.pid=b.pid) then 1 else 0 end ) from a ;

解决方案 »

  1.   

    declare @a table
    ([id] int,pid int,cid int)declare @b table
    ([id] int,pid int,cid int)insert @a
    select 1,7,5 [union] all
    select 1,8,5 [union] all
    select 1,10,5 [union] all
    select 1,21,5 [union] all
    select 1,7,6 [union] all
    select 1,6,6 insert @b
    select 1,7,5 [union] all
    select 1,8,5 [union] all
    select 1,7,6 [union] all
    select 1,6,6 select a.*,istb=case when a.pid=b.pid then 1 else 0 end
    from @a a
    left join @b b on b.[pid]=a.[pid] and b.cid=a.cid
    id          pid         cid         istb        
    ----------- ----------- ----------- ----------- 
    1           7           5           1
    1           8           5           1
    1           10          5           0
    1           21          5           0
    1           7           6           1
    1           6           6           1(所影响的行数为 6 行)
      

  2.   

    十分感谢,分不够抱歉了,csdn让我学到好多,感谢大侠们
      

  3.   

    select a.id ,a.pid,a.cid ,[istb]=(case (select 1 from a,b where a.id=b.id and 
    a.pid=b.pid)>0 then 1 else 0 end ) from a
      

  4.   

    select a.id ,a.pid,a.cid ,case b.id isnull then 0 else 1 end from a left join  b on  a.id=b.id