user2852462
user2852462

Reputation: 31

Comparing dates and times in different fields

Now, I've a problem with the following query:

SELECT * 
FROM table 
WHERE date > CURDATE() 
    OR date = CURDATE() 
    AND time > CURTIME()

It's return rows with date > of today but I need also rows with date of today but with time > of the current time.

Upvotes: 2

Views: 46

Answers (2)

Mosty Mostacho
Mosty Mostacho

Reputation: 43434

You should use the appropriate date/time functions instead of complicating yourself with complex WHERE clauses:

SELECT * FROM TABLE
WHERE ADDTIME(date, time) > NOW()

More information on the ADDTIME function in this link.

Upvotes: 0

hjpotter92
hjpotter92

Reputation: 80629

You need to put the related clauses inside parenthesis:

SELECT * 
FROM table 
WHERE date > CURDATE() 
    OR (
      date = CURDATE() 
      AND time > CURTIME()
    )

Upvotes: 1

Related Questions