找到null或value(s)

时间:2013-12-23 13:34:55

标签: tsql sql-server-2008-r2

加入存储多值信息的表格 需要查询null或value(s)
目前正在与工会合作 首先使用外连接获取null 获得价值的第二个联盟 有更好的方法吗?

select [docSVsys].[sID] 
  from [docSVsys] with (nolock) 
  left outer join [docEnum1] as [jointable] with (nolock) 
    on [jointable].[enumID] = '142' 
   and [jointable].[sID] =  [docSVsys].[sID] 
 where [jointable].[sID] is null 
Union 
Select distinct([Table].[sID]) 
  From  [DocEnum1] as [Table] with (nolock) 
 Where  1 = 1
   And  [Table].[enumID] = '142' 
   and  [Table].[valueID] in (1,2)

1 个答案:

答案 0 :(得分:1)

试试这个:

select distinct
case
    when [jointable].[valueID] in (1,2) then [jointable].[sID]
    else [docSVsys].[sID] 
end
from [docSVsys] with (nolock) 
left outer join [docEnum1] as [jointable] with (nolock) 
    on [jointable].[enumID] = '142' 
    and [jointable].[sID] =  [docSVsys].[sID] 
where [jointable].[sID] is null or [jointable].[valueID] in (1,2)

告诉我它是否正常