Home > OS >  When I add OR in SQL statement, I get rows, but it should not
When I add OR in SQL statement, I get rows, but it should not

Time:12-01

I am not sure why is this happening.

When I write my first query, it returns 0 rows - which is good.

SELECT id, dayType, employeeID 
FROM records 
WHERE companyID = 2 
  AND employeeID = 22 
  AND endTime IS NULL 

Then I found out that there are endtime with 0 AND NULL in column values so I added or endTime = 0, then it returns records

SELECT id, dayType, employeeID 
FROM records 
WHERE companyID = 2 
  AND employeeID = 22 
  AND endTime IS NULL OR endTime = 0

Can someone assist me please?

CodePudding user response:

You need to use parenthesis for the correct logic:

SELECT id, dayType, employeeID 
FROM records 
WHERE companyID = 2 
  AND employeeID = 22 
  AND (endTime IS NULL or endTime = 0);
  •  Tags:  
  • sql
  • Related