,如果你選擇了錯誤的格式或功能檢測到某個範圍的月或日數的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()。 –