Is it possible to make the year and month part of the date dynamic (based on current year and month) instead of hard coded?
SELECT SUM(`amount`)
FROM employees
WHERE (`date` between "2018-08-26" and "2018-09-26") AND `status` = 'Pending';
In the above example, if the current month is 9 then the query should be 08-26 and 09-26, if the current month is 10 then 09-26 and 10-26 and so on.
If you want to query data for last month, it's enough to use such query:
SELECT * FROM my_table
WHERE some_datetime_column > DATE_ADD(NOW(), INTERVAL -1 MONTH);
Also, if you want to get current month/year, try this:
SELECT MONTH(NOW()), YEAR(NOW())
Demo
UPDATE
Try this:
--here you specify the day of the month you want
SELECT #days := 26 - DAY(NOW());
SELECT #startDate := DATE_ADD(CAST(NOW() AS DATE), INTERVAL #days DAY),
#endDate := DATE_ADD(DATE_ADD(CAST(NOW() AS DATE), INTERVAL #days DAY), INTERVAL -1 MONTH)
Another demo
Then you can use it like:
SELECT * FROM t
WHERE date_column BETWEEN #startDate AND #endDate
Here is how you can build the desired dates:
SELECT
CURRENT_DATE,
STR_TO_DATE(CONCAT_WS('-', YEAR(CURRENT_DATE), MONTH(CURRENT_DATE), 26), '%Y-%m-%d'),
STR_TO_DATE(CONCAT_WS('-', YEAR(CURRENT_DATE), MONTH(CURRENT_DATE), 26), '%Y-%m-%d') - INTERVAL 1 MONTH
-- 2018-09-25 | 2018-09-26 | 2018-08-26
And use the above in your query:
SELECT SUM(`amount`)
FROM employees
WHERE `date` BETWEEN
STR_TO_DATE(CONCAT_WS('-', YEAR(CURRENT_DATE), MONTH(CURRENT_DATE), 26), '%Y-%m-%d') - INTERVAL 1 MONTH AND
STR_TO_DATE(CONCAT_WS('-', YEAR(CURRENT_DATE), MONTH(CURRENT_DATE), 26), '%Y-%m-%d')
AND `status` = 'Pending';
Try below with now() and last month (DATE_SUB(NOW(), INTERVAL 1 MONTH)):
SELECT SUM(`amount`) FROM employees
WHERE (`date` between DATE_SUB(NOW(), INTERVAL 1 MONTH) and now())
AND `status` = 'Pending';
To Get Current Month use
MONTH(NOW())
To Get Current Year use
YEAR(NOW())
But you don't need these
Use this-
SELECT SUM(`amount`) FROM employees
WHERE (`date` between DATE_ADD(CURDATE(), INTERVAL -1 MONTH) and CURDATE())
AND `status` = 'Pending';
Use CURDATE() to get the current date.
To get same date with previous month use:
DATE_ADD(CURDATE(), INTERVAL -1 MONTH)
As of now on 25/9/2018
SELECT CURDATE()
Output:
2018-09-25
For previous month
SELECT DATE_ADD(CURDATE(), INTERVAL -1 MONTH)
Output:
2018-08-25
Instead of raw query you can use procedure, where it will dynamically create date based on your provided date.
DELIMITER $$
-- Call Get_Sum (15) -- Dynamically you pass the date
DROP PROCEDURE IF EXISTS Get_Sum$$
DELIMITER ;
DELIMITER $$
--
-- Create procedure "Get_Sum"
CREATE DEFINER = 'root'#'localhost'
PROCEDURE Get_Sum(IN Day SMALLINT)
BEGIN
Declare current_month datetime;
Declare previous_month datetime;
Select STR_TO_DATE(CONCAT(year(now()),'-',month(NOW()),'-',Day), '%Y-%m-%d') INTO current_month;
Select DATE_SUB(current_month, INTERVAL 1 MONTH) into previous_month;
SELECT SUM(amount) FROM employees
WHERE (date between previous_month and current_month ) AND status = 'Pending';
END
$$
DELIMITER ;
Related
I want to write an sql query that must show me records that are only created last month..for example, if this month is June, I want the query to show me records of May.
This is my query but it is not working.
SELECT COUNT(*) AS stdtotal_today FROM `login`
WHERE `login_account_type` = 'STUDENT'
AND `account_created_date` = CURDATE() - 30
Note that login is the table name and account_created_date is the column name of type date.
Try This :
SELECT * FROM `login`
WHERE `login_account_type` = 'STUDENT' AND YEAR(`account_created_date`) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(`account_created_date`) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
Try this...
SELECT COUNT(*) AS stdtotal_today FROM `login` WHERE account_created_date
BETWEEN DATE_FORMAT(NOW() - INTERVAL 1 MONTH, '%Y-%m-01 00:00:00') AND DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 1 MONTH), '%Y-%m-%d 23:59:59')
I've spent all day trying to get this to work.
I have a procedure that performs calculations on dates based on an occurrence type. It returns two new datetime fields from a SELECT.
I want to use this procedure to update an events table. Something like this.
UPDATE
events
SET
start = new_start,
end = new_end
FROM CALL updateEvents(events.occurance, events.day_num, start, end)
WHERE
end < NOW();
I'm using MySQL workbench but it reports that 'FROM' is not valid input at this position.
Here is the procedure.
CREATE DEFINER=`admin`#`%` PROCEDURE `updateEvents`(occurance VARCHAR(20), day_num INT, start_time DATETIME, end_time DATETIME)
BEGIN
DECLARE time_interval INT;
DECLARE new_start DATETIME;
DECLARE last_day DATETIME;
DECLARE last_hours INT;
SET time_interval = UNIX_TIMESTAMP(end_time) - UNIX_TIMESTAMP(start_time);
SET last_hours = UNIX_TIMESTAMP(start_time) - UNIX_TIMESTAMP(DATE(start_time));
SET last_day = FROM_UNIXTIME(UNIX_TIMESTAMP(LAST_DAY(start_time)) + last_hours, "%Y-%m-%d %H:%i:%s");
SET new_start =
CASE occurance
WHEN 'daily' THEN DATE_ADD(start_time, INTERVAL 1 DAY)
WHEN 'weekly' THEN DATE_ADD(start_time, INTERVAL 1 WEEK)
WHEN 'monthly' THEN DATE_ADD(start_time, INTERVAL 1 MONTH)
WHEN 'bimonthly' THEN DATE_ADD(start_time, INTERVAL 2 MONTH)
WHEN 'quarterly' THEN DATE_ADD(start_time, INTERVAL 1 QUARTER)
WHEN 'firstof' THEN
CASE
WHEN DATE_ADD(DATE_FORMAT(start_time, '%Y-%m-01 %H:$i:$s'), INTERVAL (7+day_num - WEEKDAY(DATE_FORMAT(start_time, '%Y-%m-01 %H:$i:$s')))%7 DAY) > start_time
THEN DATE_ADD(DATE_FORMAT(start_time, '%Y-%m-01 %H:$i:$s'), INTERVAL (7+day_num - WEEKDAY(DATE_FORMAT(start_time, '%Y-%m-01 %H:$i:$s')))%7 DAY)
ELSE DATE_ADD(DATE_FORMAT(DATE_ADD(start_time, INTERVAL 1 MONTH), '%Y-%m-01 %H:$i:$s'), INTERVAL (7+day_num - WEEKDAY(DATE_FORMAT(DATE_ADD(start_time, INTERVAL 1 MONTH), '%Y-%m-01 %H:$i:$s')))%7 DAY)
END
WHEN 'secondof' THEN
CASE
WHEN DATE_ADD(DATE_FORMAT(start_time, '%Y-%m-01 %H:$i:$s'), INTERVAL (7+day_num - WEEKDAY(DATE_FORMAT(start_time, '%Y-%m-01 %H:$i:$s')))%14 DAY) > start_time
THEN DATE_ADD(DATE_FORMAT(start_time, '%Y-%m-01 %H:$i:$s'), INTERVAL (7+day_num - WEEKDAY(DATE_FORMAT(start_time, '%Y-%m-01 %H:$i:$s')))%14 DAY)
ELSE DATE_ADD(DATE_FORMAT(DATE_ADD(start_time, INTERVAL 1 MONTH), '%Y-%m-01 %H:$i:$s'), INTERVAL (7+day_num - WEEKDAY(DATE_FORMAT(DATE_ADD(start_time, INTERVAL 1 MONTH), '%Y-%m-01 %H:$i:$s')))%14 DAY)
END
WHEN 'lastof' THEN
CASE
WHEN DATE_SUB(last_day, INTERVAL ((WEEKDAY(last_day)+7-day_num))%7 DAY) > start_time
THEN DATE_SUB(last_day, INTERVAL ((WEEKDAY(last_day)+7-day_num))%7 DAY)
ELSE DATE_SUB(DATE_ADD(last_day, INTERVAL 1 MONTH), INTERVAL ((WEEKDAY(DATE_ADD(last_day, INTERVAL 1 MONTH))+7-day_num))%7 DAY)
END
ELSE
start_time
END;
SELECT
new_start,
FROM_UNIXTIME(UNIX_TIMESTAMP(new_start) + time_interval,"%Y-%m-%d %H:%i:%s") AS new_end;
END
PS. excuse my mis-spelling of occurrence.
I think you're looking for this: Can a stored procedure/function return a table?
You have to write the UPDATE statement inside of the procedure...
good luck
I have a table that contains the amount of data used each day, it looks something like this:
date | bytes
------------------
2014-01-1 | 12345
2014-01-2 | 56789
2014-01-3 | 78901
...
2014-02-1 | 12345
2014-02-2 | 56789
2014-02-3 | 78901
...
What I need to do is get the last 6 monthly totals, however the month must start on the 22nd day of the month and finish on the 21st day of the following month. For the current month it should start on the 22nd and finish today.
The best I can come up with is the following, the problem is - it is very messy and doesn't seem to give the correct result.
SELECT monthname(`date`),sum(`bytes`)
FROM `trafficDaily`
WHERE `date` between STR_TO_DATE( CONCAT( "22,", MONTH( NOW( ) )-6 , ",", YEAR( NOW( ) ) ) , "%d,%m,%Y" )
and STR_TO_DATE( CONCAT( "21,", MONTH( NOW( ) ) , ",", YEAR( NOW( ) ) ) , "%d,%m,%Y" )
group by month(DATE_SUB(`date`, INTERVAL 21 DAY))
order by `date`
Thank you in advance for your help.
You can do so by using user-defined variables to track the the change of month i.e in your case month starts from 21st
SELECT
MONTHNAME(STR_TO_DATE(group_day, '%m')) month_name ,
SUM(`bytes`) `sum`
FROM (
SELECT *,
#changemonth:= CASE
WHEN DAY(`date`) > 21
THEN #month
WHEN MONTH(`date`) <> #month
THEN #month
ELSE #month - 1
END group_day,
#month:= MONTH(`date`)
FROM
t ,(SELECT #changemonth:=0,
#month:= (SELECT MONTH(`date`) FROM t
WHERE `date` > NOW() - INTERVAL 6 MONTH ORDER BY `date` LIMIT 1) aa
) tt
WHERE `date` > NOW() - INTERVAL 6 MONTH
ORDER BY `date`
) a
GROUP BY group_day
Demo for last 3 months
Edit from comments for the case when January lies in last 6 month period
SELECT
MONTHNAME(
STR_TO_DATE(
CASE WHEN group_day < 1
THEN 12 ELSE group_day
END, '%m'
)
) month_name ,
SUM(`bytes`) `sum`
FROM (
SELECT *,
#changemonth:= CASE
WHEN DAY(`date`) > 21
THEN #month
WHEN MONTH(`date`) <> #month
THEN #month
ELSE #month - 1
END group_day,
#month:= MONTH(`date`)
FROM
t ,(SELECT #changemonth:=0,
#month:= (SELECT MONTH(`date`) FROM t
WHERE `date` > NOW() - INTERVAL 6 MONTH ORDER BY `date` LIMIT 1) aa
) tt
WHERE `date` > NOW() - INTERVAL 6 MONTH
ORDER BY `date`
) a
GROUP BY group_day
Demo with January
You have at least two options:
1st option
Create a calendar table and assign the 'business month' to each days. You can prepare your table for a long time period, then you can join to that table by date and you can do the grouping. If you have to do this calculation regularry, this is a good solution. (You can upgrade and use the calendar table to do several tasks)
2nd option
You can calculate the 'business month' by the date using the following query. (Please note, that I did not tested this query, so there could be typos).
SELECT
CASE
WHEN DAY(date) >= 22 THEN CONCAT(YEAR(date), '-', MONTH(date))
ELSE CONCAT(YEAR(date - INTERVAL 1 MONTH), '-', MONTH(date - INTERVAL 1 MONTH))
END AS m,
SUM(bytes)
FROM
log -- Use your table name instead :)
GROUP BY
CASE
WHEN DAY(date) >= 22 THEN CONCAT(YEAR(date), '-', MONTH(date))
ELSE CONCAT(YEAR(date - INTERVAL 1 MONTH), '-', MONTH(date - INTERVAL 1 MONTH))
END
You can adjust the calculation to your needs.
I need to select data from MySQL database between the 1st day of the current month and current day.
select*from table_name
where date between "1st day of current month" and "current day"
Can someone provide working example of this query?
select * from table_name
where (date between DATE_ADD(LAST_DAY(DATE_SUB(CURDATE(), interval 30 day), interval 1 day) AND CURDATE() )
Or better :
select * from table_name
where (date between DATE_FORMAT(NOW() ,'%Y-%m-01') AND NOW() )
I was looking for a similar query where I needed to use the first day of a month in my query.
The last_day function didn't work for me but DAYOFMONTH came in handy.
So if anyone is looking for the same issue, the following code returns the date for first day of the current month.
SELECT DATE_SUB(CURRENT_DATE, INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY);
Comparing a date column with the first day of the month :
select * from table_name where date between
DATE_SUB(CURRENT_DATE, INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY) and CURRENT_DATE
select * from table_name
where `date` between curdate() - dayofmonth(curdate()) + 1
and curdate()
SQLFiddle example
I have used the following query. It has worked great for me in the past.
select date(now()) - interval day(now()) day + interval 1 day
try this :
SET #StartDate = DATE_SUB(DATE(NOW()),INTERVAL (DAY(NOW())-1) DAY);
SET #EndDate = ADDDATE(CURDATE(),1);
select * from table where (date >= #StartDate and date < #EndDate);
Complete solution for mysql current month and current year, which makes use of indexing properly as well :)
-- Current month
SELECT id, timestampfield
FROM table1
WHERE timestampfield >= DATE_SUB(CURRENT_DATE, INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY)
AND timestampfield <= LAST_DAY(CURRENT_DATE);
-- Current year
SELECT id, timestampfield
FROM table1
WHERE timestampfield >= DATE_SUB(CURRENT_DATE, INTERVAL DAYOFYEAR(CURRENT_DATE)-1 DAY)
AND timestampfield <= LAST_DAY(CURRENT_DATE);
select * from table
where date between
(date_add (CURRENT_DATE, INTERVAL(1 - DAYOFMonth(CURRENT_DATE)) day)) and current_date;
select * from <table>
where <dateValue> between last_day(curdate() - interval 1 month + interval 1 day)
and curdate();
I found myself here after needing this same query for some Business Intelligence Queries I'm running on an e-commerce store. I wanted to add my solution as it may be helpful to others.
set #firstOfLastLastMonth = DATE_SUB(LAST_DAY(DATE_ADD(NOW(), INTERVAL -2 MONTH)),INTERVAL DAY(LAST_DAY(DATE_ADD(NOW(), INTERVAL -2 MONTH)))-1 DAY);
set #lastOfLastLastMonth = LAST_DAY(DATE_ADD(NOW(), INTERVAL -2 MONTH));
set #firstOfLastMonth = DATE_SUB(LAST_DAY(DATE_ADD(NOW(), INTERVAL -1 MONTH)),INTERVAL DAY(LAST_DAY(DATE_ADD(NOW(), INTERVAL -1 MONTH)))-1 DAY);
set #lastOfLastMonth = LAST_DAY(DATE_ADD(NOW(), INTERVAL -1 MONTH));
set #firstOfMonth = DATE_ADD(#lastOfLastMonth, INTERVAL 1 DAY);
set #today = CURRENT_DATE;
Today is 2019-10-08 so the output looks like
#firstOfLastLastMonth = '2019-08-01'
#lastOfLastLastMonth = '2019-08-31'
#firstOfLastMonth = '2019-09-01'
#lastOfLastMonth = '2019-09-30'
#firstOfMonth = '2019-10-01'
#today = '2019-10-08'
A less orthodox approach might be
SELECT * FROM table_name
WHERE LEFT(table_name.date, 7) = LEFT(CURDATE(), 7)
AND table_name.date <= CURDATE();
as a date being between the first of a month and now is equivalent to a date being in this month, and before now. I do feel that this is a bit easier on the eyes than some other approaches, though.
SELECT date_sub(current_date(),interval dayofmonth(current_date())-1 day) as first_day_of_month;
I had some what similar requirement - to find first day of the month but based on year end month selected by user in their profile page.
Problem statement - find all the txns done by the user in his/her financial year. Financial year is determined using year end month value where month can be any valid month - 1 for Jan, 2 for Feb, 3 for Mar,....12 for Dec.
For some clients financial year ends on March and some observe it on December.
Scenarios - (Today is `08 Aug, 2018`)
1. If `financial year` ends on `July` then query should return `01 Aug 2018`.
2. If `financial year` ends on `December` then query should return `01 January 2018`.
3. If `financial year` ends on `March` then query should return `01 April 2018`.
4. If `financial year` ends on `September` then query should return `01 October 2017`.
And, finally below is the query. -
select #date := (case when ? >= month(now())
then date_format((subdate(subdate(now(), interval (12 - ? + month(now()) - 1) month), interval day(now()) - 2 day)) ,'%Y-%m-01')
else date_format((subdate(now(), interval month(now()) - ? - 1 month)), '%Y-%m-01') end)
where ? is year end month (values from 1 to 12).
The key here is to get the first day of the month. For that, there are several options. In terms of performance, our tests show that there isn't a significant difference between them - we wrote a whole blog article on the topic. Our findings show that what really matters is whether you need the result to be VARCHAR, DATETIME, or DATE.
The fastest solution to the real problem of getting the first day of the month returns VARCHAR:
SELECT CONCAT(LEFT(CURRENT_DATE, 7), '-01') AS first_day_of_month;
The second fastest solution gives a DATETIME result - this runs about 3x slower than the previous:
SELECT TIMESTAMP(CONCAT(LEFT(CURRENT_DATE, 7), '-01')) AS first_day_of_month;
The slowest solutions return DATE objects. Don't believe me? Run this SQL Fiddle and see for yourself 😊
In your case, since you need to compare the value with other DATE values in your table, it doesn't really matter what methodology you use because MySQL will do the conversion implicitly even if your formula doesn't return a DATE object.
So really, take your pick. Which is most readable for you? I'd pick the first since it's the shortest and arguably the simplest:
SELECT * FROM table_name
WHERE date BETWEEN CONCAT(LEFT(CURRENT_DATE, 7), '-01') AND CURDATE;
SELECT * FROM table_name
WHERE date BETWEEN DATE(CONCAT(LEFT(CURRENT_DATE, 7), '-01')) AND CURDATE;
SELECT * FROM table_name
WHERE date BETWEEN (LAST_DAY(CURRENT_DATE) + INTERVAL 1 DAY - INTERVAL 1 MONTH) AND CURDATE;
SELECT * FROM table_name
WHERE date BETWEEN (DATE(CURRENT_DATE) - INTERVAL (DAYOFMONTH(CURRENT_DATE) - 1) DAY) AND CURDATE;
SELECT * FROM table_name
WHERE date BETWEEN (DATE(CURRENT_DATE) - INTERVAL (DAYOFMONTH(CURRENT_DATE)) DAY + INTERVAL 1 DAY) AND CURDATE;
SELECT * FROM table_name
WHERE date BETWEEN DATE_FORMAT(CURRENT_DATE,'%Y-%m-01') AND CURDATE;
I used this one
select DATE_ADD(DATE_SUB(LAST_DAY(now()), INTERVAL 1 MONTH),INTERVAL 1 day) first_day
,LAST_DAY(now()) last_day, date(now()) today_day
All the responses here have been way too complex. You know that the first of the current month is the current date but with 01 as the date. You can just use YEAR() and MONTH() to build the month date by inputting the NOW() method.
Here's the solution:
select * from table_name
where date between CONCAT_WS('-', YEAR( NOW() ), MONTH( NOW() ), '01') and DATE( NOW() )
CONCAT_WS() joins a series of strings with a separator (a dash in this case).
So if today is 2020-08-28, YEAR( NOW() ) = '2020' and MONTH( NOW() ) = '08' and then you just need to append '01' at the end.
Voila!
Get first date and last date from month and year.
select LAST_DAY(CONCAT(year,'.',month,'.','01')) as registerDate from user;
select date_add(date_add(LAST_DAY(end_date),interval 1 DAY),interval -1 MONTH) AS closingDate from user;
SET #date:='2012-07-11';
SELECT date_add(date_add(LAST_DAY(#date),interval 1 DAY),
interval -1 MONTH) AS first_day
I need to select all rows in my database that were created last month.
For example, if the current month is January, then I want to return all rows that were created in December, if the month is February, then I want to return all rows that were created in January. I have a date_created column in my database that lists the date created in this format: 2007-06-05 14:50:17.
SELECT * FROM table
WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
Here's another alternative. Assuming you have an indexed DATE or DATETIME type field, this should use the index as the formatted dates will be type converted before the index is used. You should then see a range query rather than an index query when viewed with EXPLAIN.
SELECT
*
FROM
table
WHERE
date_created >= DATE_FORMAT( CURRENT_DATE - INTERVAL 1 MONTH, '%Y/%m/01' )
AND
date_created < DATE_FORMAT( CURRENT_DATE, '%Y/%m/01' )
If there are no future dates ...
SELECT *
FROM table_name
WHERE date_created > (NOW() - INTERVAL 1 MONTH);
Tested.
Alternatively to hobodave's answer
SELECT * FROM table
WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
You could achieve the same with EXTRACT, using YEAR_MONTH as unit, thus you wouldn't need the AND, like so:
SELECT * FROM table
WHERE EXTRACT(YEAR_MONTH FROM date_created) = EXTRACT(YEAR_MONTH FROM CURDATE() - INTERVAL
1 MONTH)
SELECT *
FROM yourtable
where DATE_FORMAT(date_created, '%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 1 month),'%Y-%m')
This should return all the records from the previous calendar month, as opposed to the records for the last 30 or 31 days.
Even though the answer for this question has been selected already, however, I believe the simplest query will be
SELECT *
FROM table
WHERE
date_created BETWEEN (CURRENT_DATE() - INTERVAL 1 MONTH) AND CURRENT_DATE();
WHERE created_date >= DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1 DAY)
AND created_date <= DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1 MONTH)), INTERVAL 0 DAY)
This worked for me (Selects all records created from last month, regardless of the day you run the query this month)
Alternative with single condition
SELECT * FROM table
WHERE YEAR(date_created) * 12 + MONTH(date_created)
= YEAR(CURRENT_DATE) * 12 + MONTH(CURRENT_DATE) - 1
select fields FROM table
WHERE date_created LIKE concat(LEFT(DATE_SUB(NOW(), interval 1 month),7),'%');
this one will be able to take advantage of an index if your date_created is indexed, because it doesn't apply any transformation function to the field value.
Here is the query to get the records of the last month:
SELECT *
FROM `tablename`
WHERE `datefiled`
BETWEEN DATE_SUB( DATE( NOW( ) ) , INTERVAL 1
MONTH )
AND
LAST_DAY( DATE_SUB( DATE( NOW( ) ) , INTERVAL 1
MONTH ) )
Regards
- saqib
if you want to get orders from last month, you can try using
WHERE MONTH(order_date) = MONTH(CURRENT_DATE()) -1
One more way to do this in:
MYSQL
select * from <table_name> where date_created >= DATE_ADD(NOW(), INTERVAL -30 DAY);
SELECT * FROM table
WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)