MySQL:比较日期字符串?

use*_*714 2 mysql date

我有一个字符串,它包含这样的值: 11.05.2012 05:22:12

现在我想获得该列中包含a的所有行date <= 30.09.2011.我试过了

DATE_FORMAT(my_column_with_the_string_date, "%Y-%m-%d") <= '2011-09-30'
Run Code Online (Sandbox Code Playgroud)

但这失败了(没有结果).有任何想法吗?

fan*_*nts 15

你需要的是STR_TO_DATE()功能.

SELECT
*
FROM yourTable
WHERE
STR_TO_DATE(my_column_with_the_string_date, '%d.%m.%Y') <= '2011-09-30'
Run Code Online (Sandbox Code Playgroud)

在这里阅读更多相关信息.

  • 这不是性能最好的,因为你在my_column_with_the_string_date的所有单元格上进行转换.在大表格中它会非常低.你应该转换字符串 (2认同)