mysql between operator with dates

前端 未结 4 1254
栀梦
栀梦 2021-01-15 16:46
select \'2011-02-29\' BETWEEN \'2011-02-01\' AND \'2011-03-03\'‎

this is returning 1. I think between doesn\'t consider leap year.

4条回答
  •  一个人的身影
    2021-01-15 17:09

    DATE checks the validity while str_to_date does not.

    mysql> select str_to_date('2010-02-31', '%Y-%m-%d');
    +---------------------------------------+
    | str_to_date('2010-02-31', '%Y-%m-%d') |
    +---------------------------------------+
    | 2010-02-31                            |
    +---------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> select str_to_date('2010-04-31', '%Y-%m-%d');
    +---------------------------------------+
    | str_to_date('2010-04-31', '%Y-%m-%d') |
    +---------------------------------------+
    | 2010-04-31                            |
    +---------------------------------------+
    1 row in set (0.00 sec)
    mysql> select date('2010-02-31');
    +--------------------+
    | date('2010-02-31') |
    +--------------------+
    | NULL               |
    +--------------------+
    1 row in set, 1 warning (0.00 sec)
    
    mysql> select date('2010-04-31');
    +--------------------+
    | date('2010-04-31') |
    +--------------------+
    | NULL               |
    +--------------------+
    1 row in set, 1 warning (0.00 sec)
    

    UPDATE according to @Aziz , DATE will check whether a date is real or not. According to my test, seems str_to_date does not check.

提交回复
热议问题