sql case在where子句中选择项

时间:2012-02-17 21:03:50

标签: sql

我有以下声明

    SELECT b.Prig
    FROM Tirr tr
    JOIN Bud b 
    ON tr.ID = b.ID
    WHERE b.Syst = 1 and tr.Salesmen = @SalesID

在上面的陈述中,我需要做b.Syst = 1只有当b.Call不是'55'所以我需要一个案例陈述但不知道如何将其合并

我试过了:

    SELECT b.Prig
    FROM Tirr tr
    JOIN Bud b 
    ON tr.ID = b.ID
    WHERE 
    case b.Call
    when '55' then 
      b.Syst = 1 and tr.Salesmen = @SalesID
    else 
     tr.Salesmen = @SalesID

3 个答案:

答案 0 :(得分:2)

SELECT 
    b.Prig
FROM Tirr tr
JOIN Bud b 
    ON tr.ID = b.ID
WHERE tr.Salesmen = @SalesID
AND ((b.Call = '55' AND b.syst = 1) OR b.Call <> '55')

答案 1 :(得分:1)

如果你真的想要,你仍然可以使用案例

SELECT b.prig 
FROM   tirr tr 
       JOIN bud b 
         ON tr.id = b.id 
WHERE  CASE b.call 
         WHEN '55' THEN b.syst 
         ELSE 1 
       END = 1 
       AND tr.salesmen = @SalesID 

答案 2 :(得分:0)

SELECT b.Prig
FROM Tirr tr
JOIN Bud b 
ON tr.ID = b.ID
WHERE 
    (isnull(b.Call,'') != '55' and b.Syst = 1) or b.Call = '55') 
    and tr.Salesmen = @SalesID