Check if the date is up or past in MysQL

I have two columns in a table

  • event_date (value - 09/22/2013)
  • event_time (value is 7:11 PM)

Now I want to write a query that should return a string that has a time and date in the future or past

I wrote something like this

select * from events e where concat(e.event_date,' ',e.event_time) <= date_format(now(),'%m/%d/%Y %g:%i %a') 

But he did not work, this is only one of those things that you never know what is wrong with them.

Thanks in advance!

+6
source share
3 answers
 SELECT * FROM Events WHERE event_date < CURRENT_DATE() OR ( event_date = CURRENT_DATE() AND event_time <= CURRENT_TIME() ) 

This has the advantage of using any index that may exist on event_date .

+4
source
 select * from events e where TIMESTAMP(e.event_date,e.event_time) <= NOW() 
+1
source

Possible solution if event_date and event_time values ​​are saved as you showed them

Select past events

 SELECT * FROM events WHERE STR_TO_DATE(CONCAT(event_date, ' ', event_time), '%m/%d/%Y %h:%i %p') < NOW() 

Choose future events

 SELECT * FROM events WHERE STR_TO_DATE(CONCAT(event_date, ' ', event_time), '%m/%d/%Y %h:%i %p') > NOW() 

Here is the SQLFiddle demo

0
source

Source: https://habr.com/ru/post/949507/


All Articles