【问题标题】:MySQL How to use and OR inside an AND operator for a where clause?MySQL 如何在 AND 运算符中为 where 子句使用和 OR?
【发布时间】:2020-03-06 16:38:26
【问题描述】:

我尝试在 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 且不为空;

【问题讨论】:

    标签: mysql sql where-clause or-operator and-operator


    【解决方案1】:

    我想要它,所以我的结果中的 BattedBallID 不为 0 且不为空;

    你想要:

    and BattedBallID is not null and BattedBallID <> 0;
    

    代替:

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

    您的原始表达式实际上将始终评估为真,因为两个条件不能同时为假:如果某事物是null,那么它不等于0,如果某事物等于0 ,那么就不是null

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-02
      • 2014-10-15
      • 1970-01-01
      • 2012-02-22
      • 1970-01-01
      • 2020-12-03
      相关资源
      最近更新 更多