SQL Server中的多个子句,其中所有列都不等于零

问题描述 投票:2回答:1

SQL Server为例

col1, col2, col3, col4
 id1, 0.00, 0.00, 0.00
 id2, 0.00, 1.00, 0.00
 id3, 5.55, 2.22, 0.00
 id4, 0.00, 0.00, 0.00
 id5, 1.11, 2.22, -3.33

我想实现一个WHERE子句,这样当col2,col3和col4中的所有值都等于零时,该行将从结果中排除。

我尝试过以下WHERE条款 where col2!=0 and col3!=0 and col4!=0

这只返回id5行,当我所追求的是返回id2,id3和id5时。

我知道where子句是错误的,但不确定要尝试的其他东西。

我已经考虑过在列之间进行求和,但这是不可取的,因为小数可以在任一方向上进行,也可能在关闭机会等于零时,即使所有值都已填充

sql sql-server where-clause
1个回答
5
投票

你的意图是:

where not (col2=0 and col3=0 and col4=0)

解释为没有全部三个零的所有行。

要么

where col2!=0 or col3!=0 or col4!=0

它解释为至少有一列非零的所有行。

© www.soinside.com 2019 - 2024. All rights reserved.