MySQL - 在CASE语句中使用NOT IN

时间:2012-07-24 02:26:19

标签: mysql

我想以某种方式在CASE语句中使用NOT IN术语,但我收到语法错误。我想在每个特定数字(在这种情况下为14)未出现在T.Segment列中时设置TribQin = 0.

SELECT DISTINCT F.JDAY,     
    F.TL6 AS 'OriginalLayer',
    CASE
        WHEN 14 NOT IN T.Segment THEN 0
        ELSE 
                (SELECT T.flow
                FROM trib_data AS T
                WHERE T.JDAY<=F.JDAY AND T.Segment=14 AND T.Year=2000
                ORDER BY T.JDAY DESC LIMIT 1)
    END AS 'TribQin'
FROM FlexGridLayers AS F
INNER JOIN trib_data AS T
ON T.Segment=F.Segment
WHERE F.Year=2000 AND F.Segment=14
ORDER BY F.JDAY;

我得到的错误是:

[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'T.Segment THEN 0
        ELSE 
                (SELECT T.flow
                FROM trib_data AS T
                ' at line 4

3 个答案:

答案 0 :(得分:0)

使用此:

CASE
    WHEN T.Segment != 14 THEN 0
    ELSE ...

答案 1 :(得分:0)

您可以尝试以下方法:

SELECT DISTINCT k.JDAY,     
    k.TL6 AS 'OriginalLayer',
    CASE
        WHEN k.kid is not null THEN 0
        ELSE 
                (SELECT T.flow
                FROM trib_data AS T
                WHERE T.JDAY<=F.JDAY AND T.Segment=14 AND T.Year=2000
                ORDER BY T.JDAY DESC LIMIT 1)
    END AS 'TribQin'
FROM ( select F.*,F2.id as kid from FlexGridLayers F 
       left outer join FlexGridLayers F2 on F.id=F2.id and F.Segment=14) k
INNER JOIN trib_data AS T
ON T.Segment=k.Segment
WHERE k.Year=2000 AND k.Segment=14
ORDER BY k.JDAY;

查询是单独连接表和segment = 14的值,这样我们就可以知道列14中是否存在值14。

答案 2 :(得分:-1)

在MySQL中,如果

NOT

不起作用,请尝试

<>

(小于或大于,即“不是”)