Search code examples
phpmysqltwitter-oauth

Select both date and time column if MYSQL


Hi I'm trying select post where post date less than current date , AND post time less thank current time Unfortunately I put post date and post time on separate column , whenever I tried get post of current day and it time Passed

SELECT date_published,`twitter_post`,`twitter_pubstatus`,`time_published`
FROM `topics` WHERE date_published <= CURDATE() AND time_published < CURTIME()

this gives me wrong result of course as MYSQL filters Post with time_published less than current hour . In short Ho can I apply condition to select post published in current date that it time earlier than current hour , AND SELECT other post which older than current date , I tried using AS statement to filter first result (where post are older than today) , and than join it with another result (whre posts are dated today and it time passed ). this is my topics structure

twitter_post     user_id       date_published time_published
------------|-----------------|--------------|----------------
some post   |    6507763398   |  2016-04-22  |  22:00:21
other post  |    6507763398   |  2016-04-24  |  02:10:21
future post |    6507763398   |  2016-04-24  |  23:34:21

Solution

  • Use

    WHERE timestamp(date_published, time_published) <= NOW()
    

    which is short and readable but cannot make use of indexes. To make it faster use

    WHERE date_published < CURDATE()
      OR (date_published = CURDATE() AND time_published <= CURTIME())