1

I am trying to get all the rows where start date is above current date or end date is above current date and whitepaper column is null.

For some reason this sql returns what i expect EXCEPT it also returns the rows where whitepaper is NOT NULL, if i turn the sql around and say whitepaper IS NOT NULL it will return everything as expected all the ones where whitepaper is not null and not the ones where whitepaper is null?

The 'And' should it not specify that this has to be fullfilled?

SQL

SELECT id, 
       name, 
       whitepaper, 
       date_start, 
       date_end 
FROM approved 
WHERE  date_start >= CURDATE() OR date_end >= CURDATE() 
       AND whitepaper IS NULL 
ORDER BY date_end DESC 
LIMIT 10

4 Answers 4

2

As other told you, you have to use parenthesis around the OR condition.

Why ? Because the OR has a lower priority than AND

Without parenthesis, your query is in fact

 SELECT id, 
        name, 
        whitepaper, 
        date_start, 
        date_end  FROM approved  
 WHERE   date_start >= CURDATE() 
         OR
         ( 
           date_end >= CURDATE() 
           AND
           whitepaper IS NULL
         )
         ORDER BY date_end DESC  LIMIT 10
Sign up to request clarification or add additional context in comments.

Comments

1

your OR should in (),try with below:

 SELECT id, name, whitepaper, date_start, date_end 
    FROM approved 
    WHERE  (date_start >= CURDATE() OR date_end >= CURDATE())
     AND whitepaper IS NULL 
     ORDER BY date_end DESC LIMIT 10

Comments

1

Try putting your ORs in parentheses:

SELECT id, name, whitepaper, date_start, date_end 
FROM approved 
WHERE  (date_start >= CURDATE() OR date_end >= CURDATE()) AND whitepaper IS NULL 
ORDER BY date_end DESC LIMIT 10

Comments

0

Well isnull you may use along with expression also you may check empty data in whitepaper field here is my suggestion SELECT id, name, whitepaper, date_start, date_end FROM approved WHERE (date_start >= CURDATE() OR date_end >= CURDATE()) AND (ISNULL(whitepaper) or whitepaper='') ORDER BY date_end DESC LIMIT 10

1 Comment

Thanks for jumping in, but there are issues with your post. You didn't explain what you did to help the OP see his error, or what distinguishes your answer from the ones that came before you. Also, you're code isn't formatted as code. I'm not downvoting your answer, but others may unless you fix it up a little. stackoverflow.com/help/how-to-answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.