0

I'm a little unsure of how to build the following query for a MySQL database.

My table has two int fields, startDate and endDate that hold UNIX timestamps. endDate can be NULL which implies no ending date.

I want to get all records for which the current timestamp falls within the range.

In the case that endDate has a value, I can do:

SELECT * FROM table WHERE startDate < NOW() AND endDate > NOW()

and in the case that endDate is NULL, I can do:

SELECT * FROM table WHEREstartDate< NOW()

My question is, how I can combine the above two queries into one, single conditional query?

2 Answers 2

3
SELECT * FROM table 
WHERE startDate < NOW() AND (endDate IS NULL OR endDate > NOW())
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Easier than I was thinking it would be.
2

Easy:

SELECT * FROM table WHERE startDate < NOW() AND
                          (endDate IS NULL OR endDate > NOW())

1 Comment

Not sure about the && and || - do they work cross-platform? I'd tend to recommend just using AND and OR, imo.

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.