0

我想以某种方式在 CASE 语句中使用 NOT IN 术语,但出现语法错误。我想要的是在 T.Segment 列中没有出现某个数字(在本例中为 14)时设置 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
4

3 回答 3

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;

查询是连接表本身和段 = 14 的值,这样我们就可以知道值 14 是否确实存在于列中。

于 2012-07-24T02:56:32.843 回答
0

用这个:

CASE
    WHEN T.Segment != 14 THEN 0
    ELSE ...
于 2012-07-24T02:32:51.267 回答
-1

在 MySQL 中,如果

NOT

不行,试试

<>

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

于 2012-07-24T02:31:33.170 回答