MySQL - select data from database between two dates

后端 未结 8 1190
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 22:07

I have saved the dates of a user\'s registration as a datetime, so that\'s for instance 2011-12-06 10:45:36. I have run this query and I expected this item

相关标签:
8条回答
  • 2020-11-27 22:40

    Have you tried before and after rather than >= and <=? Also, is this a date or a timestamp?

    0 讨论(0)
  • 2020-11-27 22:41

    Another alternative is to use DATE() function on the left hand operand as shown below

    SELECT users.* FROM users WHERE DATE(created_at) BETWEEN '2011-12-01' AND '2011-12-06'
    

     

    0 讨论(0)
  • 2020-11-27 22:42

    Maybe use in between better. It worked for me to get range then filter it

    0 讨论(0)
  • 2020-11-27 22:50
    SELECT users.* FROM users WHERE created_at BETWEEN '2011-12-01' AND '2011-12-07';
    
    0 讨论(0)
  • 2020-11-27 22:50

    You can use MySQL DATE function like below

    For instance, if you want results between 2017-09-05 till 2017-09-09

    SELECT DATE(timestamp_field) as date FROM stocks_annc WHERE DATE(timestamp_field) >= '2017-09-05' AND DATE(timestamp_field) <= '2017-09-09'
    

    Make sure to wrap the dates within single quotation ''

    Hope this helps.

    0 讨论(0)
  • 2020-11-27 22:59

    Searching for created_at <= '2011-12-06' will search for any records that where created at or before midnight on 2011-12-06 . You want to search for created_at < '2011-12-07'.

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