Query MySQL database with timestamp

I have a timestamp [SubmissionDate] field that defaults to current_timestamp, and I was wondering how can I query my database in such a way that, for example, I only show all records submitted for a specific year and month? Sort of:

SELECT * FROM DNA_entrys
WHERE `SubmissionDate`.month = February
AND `SubmissionDate`.year = 2004

There should be an elementary operation, but I could not find a quick answer to this

+3
source share
2 answers
SELECT * FROM DNA_entrys
WHERE month(`SubmissionDate`) = 2
AND year(`SubmissionDate`) = 2004

tho i suggest:

SELECT * FROM DNA_entrys
WHERE `SubmissionDate` between '2004-02-01' and ('2004-03-01' - INTERVAL 1 SECOND)

last allows you to use indexes, which makes the query faster if you have the ithat field indexed.

+1
source

It works?

SELECT * FROM DNA_entrys
WHERE MONTH(SubmissionDate) = 2
AND YEAR(SubmissionDate) = 2004
+1
source

Source: https://habr.com/ru/post/1767588/


All Articles