有一个表A(a,b),一个表B(x,b)。
表B中的字段b引用表A里的字段b,A中的同一个b可能在B中有多条数据。
现在我要查询A表及确定A中的b字段是否出现在B中,根据此附加返回一个字段,标识有无被引用。
sql语句该怎么写???
先谢过!
表B中的字段b引用表A里的字段b,A中的同一个b可能在B中有多条数据。
现在我要查询A表及确定A中的b字段是否出现在B中,根据此附加返回一个字段,标识有无被引用。
sql语句该怎么写???
先谢过!
*,
[是否引用] = (select top(1) 1 from dbo.B where b = A.b)
from dbo.A
*,
[是否引用] = case when (select 1 from dbo.B where b = A.b)=1 then '引用' else '未引用' end
from dbo.A
*,
[是否引用] = case when exists(select 1 from dbo.B where b = A.b) then 'Yes' else 'No' end
from dbo.A
select
*,
[是否引用] =
case
when (select 1 from dbo.B where b = A.b)=1 then '引用' else '未引用'
end
from dbo.A
当(select 1 from dbo.B where b = A.b)返回值有多个会有问题的哦
select
*,
[是否引用] = case when exists (select 1 from dbo.B where b = A.b) then '引用' else '未引用' end
from dbo.A