Queries using datetime in mysql to search by month
I recently started working with a sales database and rather than using my personal favorite timestamp column type they used datetime – it took me a couple googles before I finally found the syntax to do queries to find date ranges:
SELECT * FROM `sales` WHERE MONTH(column_name) = 6 AND DAY(column_name) < 10;
Will get all the sales before the 10th in the month of June and ANY year š not so great if you have more than a years data hehe š but now you know how it works and to find year is YEAR(column_name)
If you wanted to get all records that were for this year you can use:
Select * from table_name where field_name > YEAR(CURDATE());
Categories: mysql
Comments (0)
Trackbacks (0)
Leave a comment
Trackback