Run
Run

Reputation: 57176

How to query the rows that fall in current timedate with MySQL?

How can I query the rows that fall in current timedate?

article table,

id   title   date_from                 date_to
1    a       2014-08-11 00:00:00       2014-08-25 00:00:00
2    b       2014-08-01 00:00:00       2014-08-25 00:00:00
3    c       2014-08-09 00:00:00       2014-08-10 00:00:00

query (not working obviously),

SELECT 
    p.*

FROM article AS p

WHERE p.date_from >= CURRENT_TIMESTAMP
AND p.date_to <= CURRENT_TIMESTAMP
AND p.type = 'post'

The result I am after,

1    a       2014-08-11 00:00:00       2014-08-25 00:00:00
2    b       2014-08-01 00:00:00       2014-08-25 00:00:00

Upvotes: 1

Views: 42

Answers (2)

M Khalid Junaid
M Khalid Junaid

Reputation: 64466

You can do so

SELECT 
    p.*

FROM t AS p

WHERE NOW() >= p.date_from 
AND  NOW() <= p.date_to 

Demo

Using CURRENT_TIMESTAMP

SELECT 
    p.*

FROM t AS p

WHERE CURRENT_TIMESTAMP >= p.date_from 
AND  CURRENT_TIMESTAMP  <= p.date_to 

Demo

Upvotes: 1

AlexGreg
AlexGreg

Reputation: 838

CURRENT_TIMESTAMP is the exact time when you execute that query, but you have inside you columns the begining of the day as date. try with CURRENT_DATE

Upvotes: 1

Related Questions