How to get last 2 or 3 months data in MySQL? - 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.

Related

I need to get last three month name in 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));

add days to a date column and then check if it's between two dates SQL

I need a sql query to find the data from a employee table.
Startdate is present in employee table.
I need to add 90 days in startdate and then I need to check if the startdate lies in the current month or not.
I did try below query:
SELECT *
FROM `employees`
WHERE DATE_ADD(str_to_date(startdate, '%m/%d/%Y'),INTERVAL 90 DAY) BETWEEN '09/01/2016' AND '09/30/2016'
but its not giving me the expected results.(I do have data which should show up if the query is correct.)
Hi i did change the query and ran, please see the result. I am getting results of next month too :(
This is the query
SELECT id,startdate from employees WHERE str_to_date(startdate, '%m/%d/%Y') between DATE_SUB(DATE_FORMAT(NOW(), '%Y-%m-01'), INTERVAL 90 day) -- start of this month - 90 days and DATE_SUB(LAST_DAY(NOW()), INTERVAL 90 day)
Query output
Rephrased my query...
Where the event was within the dates 90 days before the start and end of this month
WHERE str_to_date(startdate, '%m/%d/%Y') between
DATE_SUB(DATE_FORMAT(NOW(), '%Y-%m-01'), INTERVAL 90 day) -- start of this month - 90 days
and DATE_SUB(LAST_DAY(NOW()), INTERVAL 90 day) -- End of this month - 90 days
or, for three months
WHERE str_to_date(startdate, '%m/%d/%Y') between
DATE_SUB(DATE_FORMAT(NOW(), '%Y-%m-01'), INTERVAL 3 month) -- start of this month - 3 months
and DATE_SUB(LAST_DAY(NOW()), INTERVAL 3 month) -- End of this month - 3 months

Sql Query for getting last 8 days data

For getting last 8 days details I am using this condition in my query.
bill_date <= ( CURDATE( ) - INTERVAL 8 DAY )
But I am not getting the proper result.Last 8 days means 25th april - 2nd may. What is wrong in my condition and is there any another way to do the same?
Try this method
bill_date >=DATE_ADD(CURENT_DATE(),INTERVAL -8 DAY ) AND bill_date<CURRENT_DATE()
Try this
bill_date >= (sysdate - 8)
Try this to get last 8 days details
bill_date >= ( CURDATE( ) - INTERVAL 8 DAY ) --After 25th Apr
or (if you have records in future date, then use this query)
bill_date >= ( CURDATE( ) - INTERVAL 8 DAY ) AND bill_date <= CURRENT_DATE() -- From 25th Apr - 2nd May
it should be bill_date >= ( instead of <= ), because you want the result after the 25th Apr. So your condition should be bill_date >= 25th Apr.
Try this
bill_date between CURDATE() - INTERVAL 8 DAY and curdate() + interval 1 day - interval 1 second

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

Mysql date_sub interval 12 month

I'm trying to get all posts from the 12 last month, group by month. I have a quite correct query:
SELECT MONTH(time) as mois, YEAR(time) as annee, count(*) as nbre
FROM touist_stories
WHERE time >= DATE_SUB(now() + INTERVAL 1 MONTH, INTERVAL 2 YEAR)
group by MONTH(time)
order by YEAR(time) DESC, MONTH(time) DESC
But one month is always missing : november 2012
I tryied to add
+ INTERVAL 1 MONTH
to now() but it still missing... How can I get the 12 last month and not the 11 ones please?
Thanks
To get one year ago, here's a technique I've used in the past. Using #mysql variables, create a date based on the first day of a given month/year (via now()), then subtract 12 months. This example will get from Oct 1, 2012 to current -- which will include current Oct 2013. To exclude that, just add to where clause where I re-added 1 year so it goes from Oct 1, 2012 at 12:00:00 am to LESS THEN Oct 1, 2013 12:00:00.
SELECT
MONTH(time) as mois,
YEAR(time) as annee,
count(*) as nbre
FROM
touist_stories,
( select #lastYear := date_add( DATE_FORMAT(NOW(),
'%Y-%m-01'), interval -11 month) ) sqlvar
WHERE
time >= #lastYear
group by
MONTH(time)
order by
YEAR(time) DESC,
MONTH(time) DESC
Revised to make it go 11 months back (to November per example), and include UP TO AND INCLUDING all Current October activity.
For realy want on year data use 11 MONTH not 12
SELECT time
FROM touist_stories
WHERE time
BETWEEN
date_sub(Now(), INTERVAL 11 MONTH)
AND
Now();