1

I've got the following notifications table:

| id (PK - INT) | user_id (UUID) | text (Text) | show_after (Date) default NULL | 

What I'm wanting todo is select all the notifs for the user_id, I also want to ensure that we DON'T show notifs where the show_after date is in future.

So if I want to show a notification after 5 days, I'd set show_after = NOW() + 5 days.

For some reason the query I've setup is behaving weirdly and will ALWAYS return the notifications where show_after === any date, even though some of the notifs have a show_after date set in the future.

Here is the query I tried:

select * from activities where user_id = '<uuid>' OR show_after = null AND 
show_after <= NOW();

I also tried switching around the OR/ANDs without success.

0

1 Answer 1

1

You need parentheses to control the logic

select * from activities 
where user_id = '<uuid>' 
and (
     show_after <= NOW()
   OR
     show_after IS NULL
    )
;

Additionally in SQL you must use IS NULL to determine if there is no value. Using the equal symbol will not work (NULL = NULL is always false).

Sign up to request clarification or add additional context in comments.

2 Comments

Awesome! Note that I changed the date query to: show_after <= NOW() (<=) (updated this in my question )
<= makes sense, changed the answer to suit. cheers. in future queries always be on the lookout for or as it can have unexpected effects with he parentheses to limit it.

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.