简体   繁体   中英

MySQL How to use and OR inside an AND operator for a where clause?

I'm trying to use AND with OR's inside my WHERE clause but I'm not getting expected results. This is my query:

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

The problem is I'm getting BattedBallID's that have 0 in the rows but none of them have null. I want it so my results have BattedBallID's that aren't 0 and aren't null;

I want it so my results have BattedBallID's that aren't 0 and aren't null;

You want:

and BattedBallID is not null and BattedBallID <> 0;

Instead of:

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

Your original expression will actually always evaluate as true, since both conditions cannot be false at the same time: if something is null , then it is not equal to 0 , and if something is equal to 0 , then it is not null .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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