在 imdb 数据集中的 where 子句中使用时无法提取 Mysql 中的日期
unable to extract date in Mysql when using in where clause in the imdb dataset
此代码以 0 值结尾:
SELECT count(m.id), median_rating
FROM movie as m
INNER JOIN ratings as r ON m.id=r.movie_id
where median_rating=8
AND date_published between str_to_date('01-04-2018','%d-%m-%y')
AND str_to_date('2019-04-01','%y-%m-%d');
还有列:date_published 定义是:date_published 日期
所以我想从文本中提取日期格式的日期,以便我可以在过滤时使用它,但它不起作用,
我尝试使用日期的字符串格式并且代码正在运行( WHERE median_rating = 8 AND date_published BETWEEN '2018-04-01' AND '2019-04-01' )
但是当我尝试提取日期并进行比较时它不起作用:/
见https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format
%Y Year, numeric, four digits
%y Year, numeric (two digits)
使用 two-digit 年的代码与您输入的值不匹配。
演示:
select str_to_date('01-04-2018','%d-%m-%y') as date;
+------------+
| date |
+------------+
| 2020-04-01 |
+------------+
1 row in set, 1 warning (0.00 sec)
Warning (Code 1292): Truncated incorrect date value: '01-04-2018'
select str_to_date('2019-04-01','%y-%m-%d');
+--------------------------------------+
| str_to_date('2019-04-01','%y-%m-%d') |
+--------------------------------------+
| NULL |
+--------------------------------------+
1 row in set, 1 warning (0.00 sec)
Warning (Code 1411): Incorrect datetime value: '2019-04-01' for function str_to_date
如果您使用 four-digit 年 %Y
.
的格式代码,则可以解析这两个日期
此代码以 0 值结尾:
SELECT count(m.id), median_rating
FROM movie as m
INNER JOIN ratings as r ON m.id=r.movie_id
where median_rating=8
AND date_published between str_to_date('01-04-2018','%d-%m-%y')
AND str_to_date('2019-04-01','%y-%m-%d');
还有列:date_published 定义是:date_published 日期
所以我想从文本中提取日期格式的日期,以便我可以在过滤时使用它,但它不起作用, 我尝试使用日期的字符串格式并且代码正在运行( WHERE median_rating = 8 AND date_published BETWEEN '2018-04-01' AND '2019-04-01' ) 但是当我尝试提取日期并进行比较时它不起作用:/
见https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format
%Y Year, numeric, four digits
%y Year, numeric (two digits)
使用 two-digit 年的代码与您输入的值不匹配。
演示:
select str_to_date('01-04-2018','%d-%m-%y') as date;
+------------+
| date |
+------------+
| 2020-04-01 |
+------------+
1 row in set, 1 warning (0.00 sec)
Warning (Code 1292): Truncated incorrect date value: '01-04-2018'
select str_to_date('2019-04-01','%y-%m-%d');
+--------------------------------------+
| str_to_date('2019-04-01','%y-%m-%d') |
+--------------------------------------+
| NULL |
+--------------------------------------+
1 row in set, 1 warning (0.00 sec)
Warning (Code 1411): Incorrect datetime value: '2019-04-01' for function str_to_date
如果您使用 four-digit 年 %Y
.