使用和/ where子句遇到t-sql和case语句的问题

时间:2012-03-23 15:39:00

标签: sql tsql

SELECT 3 AS LoggedIn, 83 AS MessageID, Data AS DistID, 1 AS StatusID
    , 0 AS Important
    , CASE WHEN (dd.SponsorID = 3) THEN 0 ELSE d.ReceiveUplineMessages END AS Test
    , dd.SponsorID 
FROM msg_SplitVar(',', @MessageList) sv
INNER JOIN Distributor d 
    ON d.DistID = sv.Data
INNER JOIN DistributorDetail dd 
    ON dd.DistID = sv.Data
WHERE Data NOT IN (SELECT DistID FROM MessageBlockList WHERE BlockedID = 3)
    AND Test = 0

我在分配器表ReceiveUplineMessages中设置了一个字段。这可以设置为0或1。 如果他们将此设置为0我只想发送一条消息,如果它来自他们的赞助商(登录)

我的案例陈述有效,但我遇到了AND条款的问题。

splitVar函数只接受基于字符串的数组'3,4,5,6,7',并将数据拆分为插入语句的各个行。

任何人都知道我在AND条款中做错了什么?如果ReceiveUplineMessages0,我只希望它们出现。

1 个答案:

答案 0 :(得分:4)

请勿在{{1​​}}子句中使用Test别名:

WHERE

或将您的陈述包装在另一个SELECT 3 AS LoggedIn, 83 AS MessageID, Data AS DistID, 1 AS StatusID , 0 AS Important , CASE WHEN (dd.SponsorID = 3) THEN 0 ELSE d.ReceiveUplineMessages END AS Test , dd.SponsorID FROM msg_SplitVar(',', @MessageList) sv INNER JOIN Distributor d ON d.DistID = sv.Data INNER JOIN DistributorDetail dd ON dd.DistID = sv.Data WHERE Data NOT IN (SELECT DistID FROM MessageBlockList WHERE BlockedID = 3) AND (CASE WHEN (dd.SponsorID = 3) THEN 0 ELSE d.ReceiveUplineMessages END) = 0 中,以便在SELECT中使用您的考试:

WHERE