{"id":42,"date":"2014-02-26T14:43:02","date_gmt":"2014-02-26T12:43:02","guid":{"rendered":"http:\/\/en.nisi.ro\/blog\/?p=42"},"modified":"2014-07-13T18:58:01","modified_gmt":"2014-07-13T16:58:01","slug":"mysql-select-last-month-records","status":"publish","type":"post","link":"https:\/\/en.nisi.ro\/blog\/development\/mysql\/mysql-select-last-month-records\/","title":{"rendered":"MySQL: Select \u201clast month\u201d records"},"content":{"rendered":"<p>In order to select last month records you have to get first day of the last month and the last day of the first month. With this you use the mysql function\u00a0BETWEEN.<\/p>\n<p>Remember that current month could be January and the last month will in another year.<\/p>\n<p>Finding first day of the last month:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">DATE_FORMAT(CURRENT_DATE \u2013 INTERVAL 1 MONTH, &quot;%Y-%m-01&quot;)<\/pre>\n<p>Finding the last thay of the last month:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">LAST_DAY(CURRENT_DATE \u2013 INTERVAL 1 MONTH)<\/pre>\n<p>And the final Query is:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">SELECT *\r\nFROM `my_table`\r\n`DATE` BETWEEN DATE_FORMAT(CURRENT_DATE \u2013 INTERVAL 1 MONTH, &quot;%Y-%m-01&quot;) AND LAST_DAY(CURRENT_DATE \u2013 INTERVAL 1 MONTH)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In order to select last month records you have to get first day of the last month and the last day of the first month. With this you use the mysql function\u00a0BETWEEN. Remember that current month could be January and the last month will in another year. Finding first day of the last month: DATE_FORMAT(CURRENT_DATE [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[53,51,54,27,52],"class_list":["post-42","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-first-day-of-the-last-month","tag-last-month","tag-last-thay-of-the-last-month","tag-mysql-2","tag-records"],"_links":{"self":[{"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/posts\/42","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/comments?post=42"}],"version-history":[{"count":5,"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/posts\/42\/revisions"}],"predecessor-version":[{"id":47,"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/posts\/42\/revisions\/47"}],"wp:attachment":[{"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/media?parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/categories?post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/tags?post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}