Need to get records from MySQL database by date only from a datetime field

前端 未结 3 1446
后悔当初
后悔当初 2021-01-28 09:26

This seems rather simple but it has been giving me a headache. I have a column in my events table that is called \'date_time\' and it\'s type is datetime.

I need to writ

相关标签:
3条回答
  • 2021-01-28 10:00

    BETWEEN isn't safe if timestamps include fractional seconds. This is always safe:

    SELECT *
    FROM table
    WHERE date_time >= '2009-03-26 00:00:00' AND date_time < '2009-03-27 00:00:00'
    
    0 讨论(0)
  • 2021-01-28 10:06
    select * from table WHERE date(date_time) = '2009-03-27' works for me
    

    if you have an unix timestamp you could also do

    select * from table WHERE UNIX_TIMESTAMP(date_time) = your_timestamp
    
    0 讨论(0)
  • 2021-01-28 10:11
    SELECT * FROM table
    WHERE date_time BETWEEN '2009-03-27 00:00:00' AND '2009-03-27 23:59:59'
    

    Should do it.

    Alternatively, try this:

    SELECT * FROM table WHERE DATE(date_time) = '2009-03-27'
    

    The DATE() function extracts the date part of the datetime column.

    0 讨论(0)
提交回复
热议问题