我需要将 mySQL 中的 date 字段视为一个 string。为了在 LIKE 语句中使用日期:
select * from table where dob like some_string;
当前执行此操作会产生以下警告:
mysql> show warnings;
+---------+------+--------------------------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------------------------+
| Warning | 1292 | Incorrect date value: '1492' for column 'dob' at row 1 |
+---------+------+--------------------------------------------------------+
1 row in set (0.00 sec)
最佳答案
我会使用 DATE_FORMAT获取 DATE 列的字符串表示形式:
SELECT
*
FROM
yourtable
WHERE
DATE_FORMAT(dob, '%Y-%m-%d') LIKE '1492%';
并使用最适合您需要的模式。您可以在链接的手册页中找到说明符。
注意:
MySQL不能为这种查询使用索引,所以会很慢。
关于mysql - 如何在 mySQL 中将日期转换为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25319076/