How do I query between two dates using MySQL?

前端 未结 10 2241
滥情空心
滥情空心 2020-11-22 00:58

The following query:

SELECT * FROM `objects` 
WHERE (date_field BETWEEN \'2010-09-29 10:15:55\' AND \'2010-01-30 14:15:55\')

returns nothin

相关标签:
10条回答
  • 2020-11-22 01:40

    Your second date is before your first date (ie. you are querying between September 29 2010 and January 30 2010). Try reversing the order of the dates:

    SELECT *
    FROM `objects`
    WHERE (date_field BETWEEN '2010-01-30 14:15:55' AND '2010-09-29 10:15:55')
    
    0 讨论(0)
  • 2020-11-22 01:40

    Your query should have date as

    select * from table between `lowerdate` and `upperdate`
    

    try

    SELECT * FROM `objects` 
    WHERE  (date_field BETWEEN '2010-01-30 14:15:55' AND '2010-09-29 10:15:55')
    
    0 讨论(0)
  • 2020-11-22 01:41

    When using Date and Time values, you must cast the fields as DateTime and not Date. Try :

    SELECT * FROM `objects` 
    WHERE (CAST(date_field AS DATETIME) 
    BETWEEN CAST('2010-09-29 10:15:55' AS DATETIME) AND CAST('2010-01-30 14:15:55' AS DATETIME))
    
    0 讨论(0)
  • 2020-11-22 01:44

    Might be a problem with date configuration on server side or on client side. I've found this to be a common problem on multiple databases when the host is configured in spanish, french or whatever... that could affect the format dd/mm/yyyy or mm/dd/yyyy.

    0 讨论(0)
  • 2020-11-22 01:46

    Is date_field of type datetime? Also you need to put the eariler date first.

    It should be:

    SELECT * FROM `objects` 
    WHERE  (date_field BETWEEN '2010-01-30 14:15:55' AND '2010-09-29 10:15:55')
    
    0 讨论(0)
  • 2020-11-22 01:46

    DATE() is a MySQL function that extracts only the date part of a date or date/time expression

    SELECT * FROM table_name WHERE DATE(date_field) BETWEEN '2016-12-01' AND '2016-12-10';
    
    0 讨论(0)
提交回复
热议问题