简体   繁体   English

SQL不为空,作为CASE语句中的值

[英]SQL IS NOT NULL as a value in a CASE statement

I have a scenario where I want to choose non null values if B is equal to 61, I can't seem to find the correct syntax for this. 我有一种情况,如果B等于61,我想选择非null值,我似乎找不到正确的语法。

A          |        B
value1     |        61
value2     |        0 
NULL       |        61


WHERE A = CASE 
    WHEN B = 61
        THEN IS NOT NULL
    END

I want to select only the row with A=value1 我只想选择A = value1的行

为什么不直接前进呢?

WHERE (B=61) AND (A IS NOT NULL)

You can have this like: 您可以像这样:

//thats really straight forward:
WHERE (A IS NOT NULL AND B=61)

or their is another way of picking you nose :) 否则他们是另一种吸引你的方式:)

WHERE
IF(A IS NOT NULL AND B=61,1,0) //add other conditions as you like

In many situations it isn't necessary to use case expressions or IF() in a where clause, conventionally it is handled by combining conditions in parentheses. 在许多情况下,没有必要在where子句中使用大小写表达式或IF(),通常通过组合括号中的条件来处理它。

WHERE ( B = 61 AND A IS NOT NULL )

Here both conditions must be true for the combined condition to be true 这两个条件都必须为真,组合条件才为真

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM