A表aid 唯一性主键。
B表中有aid为外键不唯一。想列出A表记录并带一字段指示是否存在于B表。
aid flag 
1    true
2    false

解决方案 »

  1.   


    update a
    set flag=case when b.aid is not null then 'true' else 'false' end
    from 
    a
    left join 
    b on a.aid=b.aid
      

  2.   

    a表中没有flag字段。我只要一个select语句
      

  3.   

    select a.aid ,
    flag=case when b.aid is not null then 'true' else 'false' end from a,b
    where
    a.aid=b.aid
      

  4.   


    select a.aid,flag=case when b.aid is null then 'false' else 'true' end
    from A表 a
    left join (select distinct aid from B表) b
    on a.aid=b.aid
      

  5.   

    a表中数据如
    aid
    1
    2
    3b表中
    1
    2
    2我想得到输出aid flag
    1    true
    2    true
    3    false
      

  6.   

    select a.aid ,
    flag=case when b.aid is not null then 'true' else 'false' end 
    from a left join b on a.aid=b.aid
      

  7.   

    select a.aid ,
    flag=case when b.aid is not null then 'true' else 'false' end 
    from a left join b on a.aid=b.aid
    group by a.aid, flag
      

  8.   

    --> By dobear_0922(小熊) 2008-10-10 15:08:58
    --> 测试数据:@a
    declare @a table([aid] int)
    insert @a
    select 1 union all
    select 2 union all
    select 3
    --> 测试数据:@b
    declare @b table([aid] int)
    insert @b
    select 1 union all
    select 2 union all
    select 2select aid,flag=isnull((select top 1 'true' from @b where aid=a.aid), 'false') 
    from @a a /*
    aid         flag
    ----------- ----
    1           true
    2           true
    3           fals
    */
      

  9.   

    select aid,flag=isnull((select top 1 cast('true' as varchar(8)) from b where aid=a.aid), 'false') 
    from a /*
    aid         flag
    ----------- --------
    1           true
    2           true
    3           false
    */