如何在php mysql中只比较日期字段中的年和月

如何在php mysql中只比较日期字段中的年和月,mysql,sql,Mysql,Sql,我有一个日期字段,其中包含的数据有2017-01-10,2017-02-12,2017-05-14等等。在这里,我如何检查字段中是否有2017年10月的日期(忽略日期)?我建议您使用如下不等式: where datefield >= '2017-10-01' and datefield < '2017-11-01' where year(datefield) = 2017 and month(datefield) = 10 那么MySQL将不使用索引。(这同样适用于yearmon

我有一个日期字段,其中包含的数据有
2017-01-10
2017-02-12
2017-05-14
等等。在这里,我如何检查字段中是否有2017年10月的日期(忽略日期)?

我建议您使用如下不等式:

where datefield >= '2017-10-01' and datefield < '2017-11-01'
where year(datefield) = 2017 and month(datefield) = 10

那么MySQL将不使用索引。(这同样适用于
yearmonth()
和格式化列。)

我建议您使用如下不等式:

where datefield >= '2017-10-01' and datefield < '2017-11-01'
where year(datefield) = 2017 and month(datefield) = 10

那么MySQL将不使用索引。(这同样适用于
yearmonth()
和格式化列。)

我不认为声明的副本是真正的副本。一方面,答案集中在当前日期,但这是一个任意的日期。而且,被接受的答案不是最好的答案。我认为声明的副本不是真正的副本。一方面,答案集中在当前日期,但这是一个任意的日期。而且,被接受的答案不是最好的答案。