Quentin Lamotte Quentin Lamotte - 9 months ago 40
MySQL Question

SQL query datetimes in a given day

I have a table named "event" and the column I'm focusing on is named "start", it is DateTime-formated.
I simply need to fetch, in this table, all events which "start" date is within a given date's day.

For instance, here is a set of dates:

  • 2016-10-17 08:00:00

  • 2016-10-17 12:30:00

  • 2016-10-18 09:15:00

My query in plain English would be: "Select all events where the start date is on the same day as '2016-10-17 16:45:27' ". The database should then return both first entries.

What I'm wondering is if there are some SQL functions which could do that or if I should use som code in my PHP page to compute stuff and create a completely diffrent query... Any help is welcome!

Answer Source

The Date() function extracts the date part of a date or date/time expression.

"SELECT * FROM `event` WHERE date(start) = date('2016-10-17 16:45:27')";