我有一个表,其日期存储为文本而不是date_time。我想选择今天发生的行。我可以对日期进行硬编码,但是很明显我想动态地进行编码。SELECT * FROM view WHERE anniversary LIKE '%09-07'
SELECT * FROM view WHERE anniversary LIKE '%' + DATE_FORMAT(now(), "%m-%d")
最佳答案
可以使用月和日运算符:
SELECT * from view WHERE MONTH(anniversary) = MONTH(NOW()) AND DAY(anniversary) = DAY(NOW())
如果列的类型为
varchar
,则可以使用STR_TO_DATE
函数将其转换为日期:SELECT * from view WHERE MONTH(STR_TO_DATE(anniversary,'%Y-%m-%d')) = MONTH(NOW()) AND DAY(STR_TO_DATE(anniversary,'%Y-%m-%d')) = DAY(NOW())
关于mysql - 像日期功能一样,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57834870/