I have a table with following columns
(name : varchar), (mydate : DATE), (endtime : TIME)
I’m trying to get specific rows based on the following conditions:
- mydate is within a certain range
- mydate > date(NOW())
- endTime > time(NOW()), this should apply only to “todays” date rows
The problem is with endTime. For example, consider the following data
- apples 2011-02-01 21:00:00
- orange 2011-03-02 10:00:00
- grapes 2011-05-10 11:00:00
If the inputs to the query are
- range: 2011-01-01 to 2011-04-01
- date(NOW()) : 2011-01-01
- time(NOW()) : 20:00:00
Consider the query
SELECT name FROM mytable
WHERE mydate BETWEEN '2011-01-01' AND '2011-04-01'
AND date >= DATE(NOW())
AND endTime > TIME(NOW())
The query results only ‘apples’. But the correct result is ‘apples’ and ‘orange’.
The problem being condition: endTime > time(NOW()), this should apply only to “todays” date rows.
I dont want to UNION, as my original query is big.
Is there a way we can apply condition to certain rows only.
Any ideas are appreciated.
Thanks.
Use:
The ADDTIME function will allow you to combine the DATE and TIME value(s) into a single DATETIME, which you can then use against NOW()/CURRENT_TIMESTAMP.
Frankly, the additional criteria is redundant…