SQL server CASE select all but if value is a then other column value cannot be NULL

SQL server CASE select all but if value is a then other column value can not be NULL

您好,我需要 select a 列中的所有内容,但如果值为 1,则 b 列中的值不能为 NULL。你如何在 WHERE 子句中写这个?

你可以这样写:

where a <> 1 or b is not null

或等同于:

where not (a = 1 and b is null)

注意:这些假设 a 不为空。要处理这个问题,您可以对其进行显式测试:

where a <> 1 or a is null or b is not null

你可以写成:

其中(a = 1 AND b 不为空)