Select current months records mysql from timestamp column

前端 未结 10 2109
一整个雨季
一整个雨季 2020-12-05 15:31

I have a mysql DB that has a TIMESTAMP field titled date. How can I select all fields where the month is the current month?

Thanks in advance!

10条回答
  •  猫巷女王i
    2020-12-05 16:05

    If you want indexes to be used, don't apply any function to the column:

    SELECT * 
    FROM tableX
    WHERE `date` >= UNIX_TIMESTAMP((LAST_DAY(NOW())+INTERVAL 1 DAY)-INTERVAL 1 MONTH)
      AND `date` <  UNIX_TIMESTAMP(LAST_DAY(NOW())+INTERVAL 1 DAY) ;
    

    The functions used can be found in MySQL docs: Date and Time functions

提交回复
热议问题