,如果你选择了错误的格式或功能检测到某个范围的月或日数的STR_TO_DATE将返回空值。在你的例子中,你选择了一个错误的格式%y应该是%Y。以下是可能发生的一些示例。 (注:STR_TO_DATE差这一点,并不会检查多少天了一个月的有效期)
MariaDB [sandbox]> create table t (dt text);
Query OK, 0 rows affected (0.28 sec)
MariaDB [sandbox]> insert into t values ('2017-13-01'),('2017-04-31'),('2017-04-33'),('2017-04-03'),('birth_date');
Query OK, 5 rows affected (0.02 sec)
Records: 5 Duplicates: 0 Warnings: 0
MariaDB [sandbox]>
MariaDB [sandbox]> select str_to_date(dt,'%y-%m-%d') Invalid_year_format,
-> str_to_date(dt,'%Y-%m-%d') Valid_year_format_MM_and_DD,
-> datediff(now(),str_to_date(dt,'%Y-%m-%d')) datediff_examples
-> from t;
+---------------------+-----------------------------+-------------------+
| Invalid_year_format | Valid_year_format_MM_and_DD | datediff_examples |
+---------------------+-----------------------------+-------------------+
| NULL | NULL | NULL |
| NULL | 2017-04-31 | -6 |
| NULL | NULL | NULL |
| NULL | 2017-04-03 | 22 |
| NULL | NULL | NULL |
+---------------------+-----------------------------+-------------------+
5 rows in set, 11 warnings (0.00 sec)
使用此,DATEDIFF(NOW(),DATE(column_14)) –
相同的结果..... –
您的日期列有ymd格式,因此请尝试一次,如DATEDIFF(NOW(),column_14)不带DATE()。 –