本文介绍了选择日期范围MySQL与date_format的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
SELECT MvtDate,date_format(MvtDate,'%d - %m-%Y')
FROM(`immmvt`)
WHERE date_format(MvtDate,'%d-%m-%Y')BETWEEN '01 -01-2010'AND '02 - 01-2010'
mvtDate
type is date像 2010-01-01 00:00:00
。
当我运行查询时,结果适用于几天和几个月,但也显示了其他年份的其他结果。
喜欢 01-01-2011
等等。
解决方案
您应该使用 STR_TO_DATE
code> string 返回日期
SELECT MvtDate,date_format(MvtDate,'%d-%m-%Y')
FROM`immmvt`
WHERE MvtDate BETWEEN STR_TO_DATE('01 -01-2010','%d- %m-%Y')AND
STR_TO_DATE('02 -01-2010','%d-%m-%Y')
FYI:DATE_FORMAT()将日期转换为格式化的字符串表示形式。
STR_TO_DATE()将格式化的字符串转换回日期
I've got an issue selecting a date range with MySQL.
SELECT MvtDate,date_format(MvtDate,'%d-%m-%Y')
FROM (`immmvt`)
WHERE date_format(MvtDate,'%d-%m-%Y') BETWEEN '01-01-2010' AND '02-01-2010'
mvtDate
type is date like 2010-01-01 00:00:00
.
When I run the query, the result works for the days and the months but it also show me other result from other years.
Like 01-01-2011
etc.
解决方案
You should use STR_TO_DATE
since you want to convert string
back to date
SELECT MvtDate, date_format(MvtDate,'%d-%m-%Y')
FROM `immmvt`
WHERE MvtDate BETWEEN STR_TO_DATE('01-01-2010','%d-%m-%Y') AND
STR_TO_DATE('02-01-2010','%d-%m-%Y')
FYI: DATE_FORMAT() converts date to formatted string representation. STR_TO_DATE() converts formatted string back to date
这篇关于选择日期范围MySQL与date_format的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!