I need to get last three month name in mysql - mysql

I need to print previous three month name with this function
SELECT MonthName(CURRENT_DATE()) as month
With this query, I am getting the following :
month
September
I need to show like this it is possible
month
September
August
July
June

Use DATE_SUB to subtract date with the help of interval
SELECT MONTHNAME(DATE_SUB(curdate(), INTERVAL 1 MONTH)) as first, MONTHNAME(DATE_SUB(curdate(), INTERVAL 2 MONTH)) as second, MONTHNAME(DATE_SUB(curdate(), INTERVAL 3 MONTH)) as third

Use DATE_ADD function with MONTH interval -1 thrice to get previous 3 months name. And then use UNION ALL.
Query
select MonthName(CURRENT_DATE()) as month
union all
select MonthName(DATE_ADD(CURRENT_DATE(), INTERVAL -1 MONTH))
union all
select MonthName(DATE_ADD(CURRENT_DATE(), INTERVAL -2 MONTH))
union all
select MonthName(DATE_ADD(CURRENT_DATE(), INTERVAL -3 MONTH));

Related

MySQL Query to get all rows for 2 months ago

I need to do a select where I can chose to see results for current month, previous month, 1 month ago, 2 months ago, 3 months ago.
I found this question: MySQL: Query to get all rows from previous month, but I'm stuck with a filter that will get me all the results for 2 months ago from first to last day of the month.
I tried with this but it doesn't work:
SELECT * FROM table
AND MONTH(date_created) = MONTH(1 MONTH - INTERVAL 2 MONTH);
Try this:
SELECT * FROM table
WHERE MONTH(date_created) = MONTH(NOW() - INTERVAL 2 MONTH)
AND (
YEAR(date_created) = YEAR(NOW())
OR
YEAR(date_created) = YEAR(NOW() - INTERVAL 2 MONTH)
);
Returning records CREATED PRIOR the last 2 months only in MySQL.
If you want all rows from 2 months ago, then use logic like this:
WHERE date_created >= DATE_SUB(DATE_SUB(CURDATE(), 1 - DAY(CURDATE())), INTERVAL 2 MONTH) AND
date_created < DATE_SUB(DATE_SUB(CURDATE(), 1 - DAY(CURDATE())), INTERVAL 1 MONTH)
What is this doing? First, it is only applying functions to the current date part of the expression. This allows MySQL to use an index on date_created, if available and appropriate.
The expression DATE_SUB(CURDATE(), 1 - DAY(CURDATE()) is simply a way to get the first day of the month.
You query have an error, correct one would be:
SELECT * FROM table
WHERE MONTH(date_created) = MONTH(DATE_SUB(NOW(),INTERVAL 2 MONTH))
For current month just MONTH(NOW()), replace "2" with any number of months you need (1,3,.. 23)
as mentioned in comments this solution ignores YEAR differences, it just selects all records with the same month, no matter the year
you can filter wrong year results with additional clause:
AND YEAR(date_created) = '2019' # or year you need
Or use more complex query:
SELECT * FROM table
where
date_created between
/* first day of -2 month*/
date_sub(date_sub(now(),interval 2 month), interval (day(now())-1) day)
and
/* last day of -2 month*/
last_day(date_sub(now(),interval 2 month))

How to select records starting from last month to 12 months ago?

I need to select all the records starting from last month to 12 months ago. So when now is 2015-07-09, I would need to select records between 2014-08-01 and 2015-06-30.
Use this query :
select * from table_name where date_column between date_sub(ADDDATE(LAST_DAY(SUBDATE(curdate(), INTERVAL 11 MONTH)), 1), interval 1 month) , last_day(date_sub(curdate(), interval 1 month))
This would give you up to the end of last month.
select ... from table where mydate < date_sub(curdate(), interval day(curdate())-1 day)
This should point you in the right direction for what to learn about date math so you can figure out what to do to also test for beginning of 12 months ago (hint: subtract 12 months from the expression I have above and test for greater than or equal to that).

How to get last 2 or 3 months data in MySQL?

I m trying to get last 2 and 3 months data.
e.g Last 2 months data means Dec 2014 To Jan 2015 and 3 months means Nov 2014 TO Jan 2015
I tried
SELECT * FROM d_jobs WHERE `job_date` >= DATE_ADD( NOW( ) , INTERVAL -1 MONTH )
Here Its Count last 30 days data only .
So Which is the best way to perform this ?
Try this:
SELECT * FROM d_jobs WHERE `job_date` >= last_day(now()) + interval 1 day - interval 3 month;
Try with this query hope you will overcome your problem i think
select * from d_jobs where job_date < Now() and job_date > DATE_ADD(Now(), INTERVAL- 3 MONTH);
select * from d_jobs where job_date BETWEEN SUBDATE(CURDATE(), INTERVAL 3 MONTH) AND NOW();
select * from d_jobs WHERE job_date BETWEEN STR_TO_DATE('2012-09-01', '%Y-%m-%d') AND NOW();
Get left 7 chars of current date in ISO format (i.e. '2015-01'). Then add "-01" for 1st day of month (result is "2015-01-01"). Then use INTERVAL ... MONTH to get last months.

how to get the last three months of last year using mysql?

How would I go about retrieving records from the last three months of the previous year? I was thinking it would be:
Date_add(curdate() , interval '-1 2' year_month)
Try this:
WHERE my_date_column BETWEEN
SUBDATE(CURDATE(), INTERVAL DAYOFYEAR(CURDATE()) + 91 DAY) AND
SUBDATE(CURDATE(), INTERVAL DAYOFYEAR(CURDATE()) DAY)
91 is one less than 31 + 30 + 31, because BETWEEN is inclusive.
Note that if your column is a datetime type, you'll need the end value to be the last second of the day:
SUBDATE(SUBDATE(CURDATE(), INTERVAL DAYOFYEAR(CURDATE()) - 1 DAY), INTERVAL 1 SECOND)
See SQLFiddle of these expressions generating correct values.
Assuming you date column is named "date", something like:
SELECT
*
FROM
table
WHERE
YEAR(date) = YEAR(DATE_SUB(CURDATE(), INTERVAL 1 YEAR))
AND
MONTH(date) BETWEEN 10 AND 12

Get previous 3 months data based on current date - MySQL

How do I get the previous 3 months data using SQL statement? for example if the current date is 01/01/2012, so how to get the data for December, November, October 2011 ?
Please someone guide me.
SELECT * FROM table1
WHERE mydate BETWEEN DATE_SUB(now(), INTERVAL 3 MONTH) AND now()
Or if you want to stay within the months
SELECT * FROM table1
WHERE MONTH(mydate) BETWEEN MONTH(DATE_SUB(now(), INTERVAL 3 MONTH)) AND MONTH(now())
AND YEAR(mydate) BETWEEN YEAR(DATE_SUB(now(), INTERVAL 3 MONTH)) AND YEAR(now())
This latter version will run much slower, because it cannot use an index for mydate however.