有a ,b两个表 ,并且a,b两个表是有关联的。我想实现查询 b表时  (select b.name,(?) from b)"?" 代表b的id 只要在a 表中出现过就是true 反之就false 怎么实现??因为数据量大,要考虑性能!

解决方案 »

  1.   

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

  2.   

    select  b.name
    ,case when exists(select 1 from a where id=b.id) then 'true'  else 'false' end
    from b   
      

  3.   

    select b.name,isidina=case when exists(select 1 from a where charindex(','+ltrim(id)+',',','+b.aid+',')>0 then 'true' else 'flase' end 
    from b
      

  4.   

    select
     b.name,case when exists(select 1 from a where id=b.id) then 'true'  else 'false' end
    from
     b   
      

  5.   

    select b.name,case when a.id is null then 'false' else 'true' end
    from b
    left join (select id from a group by id) b
    on a.id=b.id
      

  6.   

    看来我想多了,我以为B表的ID像字符串型的样式('3,5,7,8')
      

  7.   

    create table #A
    (
      ID int identity(1,1) primary key,
      BID int,
      AName nvarchar(30)
    )
    insert into #A select 1,'aa'
    insert into #A select 2,'bb'
    insert into #A select 3,'cc'
    insert into #A select 4,'dd'create table #B
    (
      ID int identity(1,1) primary key,
      AID int,
      BName nvarchar(30)
    )
    insert into #B select 1,'aaaaa'
    insert into #B select 33,'bbbbb'
    insert into #B select 31,'ccbbbb'
    insert into #B select 4,'dddddd'select A.BID,case when B.AID=A.BID then 'true' else 'false'  end
    from #A A
    left join #B B
    on B.AID=A.BIDBID         
    ----------- -----
    1           true
    2           false
    3           false
    4           true(4 行受影响)
      

  8.   

    要是有 group by 分组 这样就行不通了