有一个表A(a,b),一个表B(x,b)。
表B中的字段b引用表A里的字段b,A中的同一个b可能在B中有多条数据。
现在我要查询A表及确定A中的b字段是否出现在B中,根据此附加返回一个字段,标识有无被引用。
sql语句该怎么写???
先谢过!

解决方案 »

  1.   

    select
    *,
    [是否引用] = (select top(1) 1 from dbo.B where b = A.b)
    from dbo.A
      

  2.   

    select
        *,
        [是否引用] = case when (select  1 from dbo.B where b = A.b)=1 then '引用' else '未引用' end
    from dbo.A
      

  3.   

    select
        *,
        [是否引用] = case when exists(select  1 from dbo.B where b = A.b) then 'Yes' else 'No' end
    from dbo.A
      

  4.   


    select
        *,
        [是否引用] =
         case 
         when (select  1 from dbo.B where b = A.b)=1 then '引用' else '未引用' 
         end
    from dbo.A
      

  5.   


    当(select  1 from dbo.B where b = A.b)返回值有多个会有问题的哦
      

  6.   

    感谢各位帮助小弟解决了问题,thank U all
      

  7.   


    select
        *,
        [是否引用] = case when exists (select  1 from dbo.B where b = A.b) then '引用' else '未引用' end
    from dbo.A