如何选择多列匹配单个值其他列值为空?

时间:2011-03-19 06:31:15

标签: sql sql-server

这是我的表格值

masterid   dateorder    Ist      IInd    IIIrd  IVth    Vth     VIth
------------------------------------------------------------------------
08mcaao1     1          MC1701   MC1702  MC1703 Mc1704  Mc1705  Mc1701
08mcaao1     2          MC1701   MC1702  MC1703 Mc1703  Mc1705  Mc1702 
08mcaao1     3          MC1701   MC1703  MC1703 Mc1704  Mc1705  Mc1701      

如果我根据列值(如MC1701

)选择此表

结果必须是此表格

masterid dateorder    Ist     IInd   IIIrd    IVth   Vth     VIth 
-------------------------------------------------------------------
08mcaao1    1         MC1701    -     -        -      -      Mc1701 
08mcaao1    2         MC1701    -     -        -      -      - 
08mcaao1    3         MC1701    -     -        -      -      Mc1701          

2 个答案:

答案 0 :(得分:1)

在字段Ist,IInd,IIIrd,IVth,Vth,VIth上使用CASE statement将其与您的标准进行比较。如果符合您的条件,则显示值,否则显示-

答案 1 :(得分:0)

declare @param as nvarchar(100)
set @param = 'whatever'
select
... 
case when Ist = @param then Ist else '-' end 'Ist'
...
where Ist = @param or ...