MySQL如何在where子句的and运算符中使用和OR



我试图在WHERE子句中使用AND和OR,但没有得到预期的结果。这是我的问题:

select * 
from Pitches 
where BatterID = @playerID 
and YEAR(GameDate) in (2019) 
and (BattedBallID is not null or BattedBallID <> 0);

问题是我得到的BattedBallID的行中有0,但没有一个为null。我想要它,这样我的结果就有BattedBallID,它们不是0,也不是null;

我想要它,这样我的结果就有不为0且不为null的BattedBallID;

您想要:

and BattedBallID is not null and BattedBallID <> 0;

代替:

and (BattedBallID is not null or BattedBallID <> 0);

您的原始表达式实际上总是计算为true,因为这两个条件不能同时为false:如果某个东西是null,则它不等于0,如果某个等于0,则它不是null

最新更新