仅将日期和月份与 mysql 中的日期字段进行比较

2021-11-20 00:00:00 sql date select mysql

如何在mysql中仅将日期和月份与日期字段进行比较?例如,我在一张表中有一个日期:2014-07-10

How to compare only day and month with date field in mysql? For example, I've a date in one table: 2014-07-10

同样,另一个表中的另一个日期 2000-07-10.我只想比较日期字段上的日和月是否相等.

Similarly, another date 2000-07-10 in another table. I want to compare only day and month is equal on date field.

我试过这种格式.但我无法得到答案

I tried this format. But I can't get the answer

select *
from table
where STR_TO_DATE(field,'%m-%d') = STR_TO_DATE('2000-07-10','%m-%d')
and id = "1"

推荐答案

改用 DATE_FORMAT:

Use DATE_FORMAT instead:

SELECT DATE_FORMAT('2000-07-10','%m-%d')

收益

07-10

这是您用 DATE_FORMAT() 重写的查询:

Here's your query re-written with DATE_FORMAT():

SELECT *
FROM table
WHERE DATE_FORMAT(field, '%m-%d') = DATE_FORMAT('2000-07-10', '%m-%d')
AND id = "1"

相关文章