MySQL 选择年和月的第一天

2021-12-19 00:00:00 sql date select mysql

如何在 SELECT 中找到一年的第一天?

How to find first day of year in SELECT?

SELECT `DATE`,`SomeValue1`,`SomeValue2`
FROM `SomeTableName`
WHERE (`DATE` >= [...first day of the year in date format...])

我找到了这个月 - 但我对一年的掌握还不够:(我正在寻找这个用于单独查询以查找月初和现在之间的数据)

I found this for month - but I don't QUITE have the grasp enough for year: (I was looking for this for a separate query to find data between beginning of month and now)

WHERE (`DATE` between  DATE_FORMAT(NOW() ,'%Y-%m-01') AND NOW() ) 

推荐答案

我认为您需要:

WHERE (`DATE` between  DATE_FORMAT(NOW() ,'%Y-01-01') AND NOW() ) 

老实说,你可以这样做:

To be honest, you could do:

WHERE (`DATE` between  DATE_FORMAT(NOW() ,'%Y') AND NOW() ) 

相关文章