为连接表中存在的Sql中的每一行返回位

时间:2011-08-04 21:52:38

标签: sql-server tsql

我想为在连接表中作为外来ke存在的每个rowID返回一个额外的计算列作为位。例如:Select PId, PName from Part where PId in (Select distinct FkPid in joined Part

结果应该像Pid | PNAME |位|

由于

2 个答案:

答案 0 :(得分:2)

Select PId, PName, CAST(CASE WHEN B.fkPid IS NULL THEN 0 ELSE 1 END AS BIT) ExistsOtherTable
from Part A
LEFT JOIN (Select distinct FkPid FROM [joined Part]) B
ON A.PId = B.fkPid

答案 1 :(得分:0)

只需左转加入!!!!

Select PId, PName, cast (isnull(OtherTable.FkPid,0) as bit) as [Bit]  from Part left join 
OtherTable on Part.PId=OtherTable.FkPid