MySQL - order by one month from today - mysql

How to order records by one month from today. I have tried the following but it does not work.
WHERE MONTH(date) = MONTH(CURDATE() -1 month)

Do you mean this? This will get you everything in your table where the month of the date field is equal to the current month - 1:
WHERE MONTH(date) = month(curdate() - interval 1 month)

WHERE MONTH(date) = MONTH(CURDATE() - INTERVAL 1 MONTH)

Hard to tell what you're after from your question, so I'm taking a punt that you want to show records that have the date that is exactly one month from now
SELECT *
FROM yourtable
WHERE MONTH(date) = MONTH(DATE_SUB(CURDATE(), INTERVAL 1 MONTH))
That will get all entries for October, if we are currently in November.
If you want it down to the day, so if today is November 20 you want to grab items from October 20, you'll need something like this
SELECT *
FROM agentjobsets
WHERE DATE(created_at) = DATE_SUB(CURDATE(), INTERVAL 1 MONTH)

Related

Mysql retrieve orders from last year in the same month as today

I have a table orders and I would like to make a request to retrieve all orders from the month of last year to the same month as now. Example: we are in November, I would like to recover all the orders of the month of November 2017.
I try something like that but it does not work:
SELECT COUNT(*)
FROM ps_orders o
WHERE YEAR(o.date_add) = YEAR(NOW() - INTERVAL 1 YEAR)
AND MONTH(o.date_add) = MONTH(NOW() - INTERVAL 1 MONTH)
Thank you for your help
You dont need to subtract the month. It should be MONTH(NOW()) to get the same month results from the previous year.
SELECT COUNT(*)
FROM ps_orders o
WHERE YEAR(o.date_add) = YEAR(NOW() - INTERVAL 1 YEAR)
AND MONTH(o.date_add) = MONTH(NOW()) -- same as current month
I would recommend doing this with date ranges:
SELECT COUNT(*)
FROM ps_orders o
WHERE o.date_add >= (curdate() - interval (day - 1) day) - interval 12 month AND
o.date_add >= (curdate() - interval (day - 1) day) - interval 11 month ;
When you use functions such as YEAR() and MONTH() the optimizer will not take full advantage of an available index on date_add. However, this version can use an appropriate index.

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))

MySQL (now() - Interval 1 Month) for this year only

I need some help with this.
I have the following SQL statement
SELECT * FROM table
WHERE MONTH(ORDERDATE) = MONTH(NOW() - INTERVAL 1 MONTH)
The problem is that it is returning data from last month but for all years... Am I doing something wrong or is this a normal issue that people face with this function?
The problem I am facing is that if I use the YEAR(NOW()) the report I am writing will not show the data for 2016 when we hit 2017. I'm trying to write a 6 month sales history report.
Any guidance would be appreciated.
Added Information
SELECT * FROM DATA_WH.SALESORD_HDR WHERE MONTH(ORDERDATE) = MONTH(NOW() - INTERVAL 1 MONTH)
RETURNS....
'2015-08-14 00:00:00'
Try using DATE_SUB with BETWEEN:
SELECT *
FROM DATA_WH.SALESORD_HDR
WHERE ORDERDATE BETWEEN DATE_SUB(NOW(), INTERVAL 1 MONTH) AND
DATE_SUB(NOW(), INTERVAL 2 MONTH)
This avoids the problem of having to deal with boundary conditions when using MONTH and YEAR.
Edit:
The above query will return records whose order date is between one and two months old. If you want to identify orders from the previous calendar month, then you will have to do a bit more work. Try this query:
SELECT *
FROM DATA_WH.SALESORD_HDR
WHERE ORDERDATE >= STR_TO_DATE(CONCAT('01-', LPAD(MONTH(DATE_SUB(NOW(), INTERVAL 1 MONTH)), 2, '0'), '-', YEAR(DATE_SUB(NOW(), INTERVAL 1 MONTH))), '%d-%m-%Y') AND
ORDERDATE < STR_TO_DATE(CONCAT('01-', LPAD(MONTH(NOW()), 2, '0'), '-', YEAR(NOW())), '%d-%m-%Y')
The strategy here is to build the date boundaries (August 1 and September 1 of 2016, as of the time of writing this answer), using the ORDERDATE.
Here is a Fiddle showing this logic in action:
SQLFiddle
I think you have to add another condition with AND with YEAR function
SELECT * FROM DATA_WH.SALESORD_HDR WHERE MONTH(ORDERDATE) = MONTH(NOW() - INTERVAL 1 MONTH) AND YEAR(ORDERDATE)= YEAR(NOW());
I have been working on this kind of queries recently:
This query will get the first day of the month and the last day of the month , also if you can carefully check the query you should be able to see that I have add a function from mysql that check the last day if the month "last_day(now())"
SELECT *
FROM DATA_WH.SALESORD_HDR
WHERE ORDERDATE BETWEEN date(concat(year(now()) , '-' ,month(now()) , '-01')) ,AND
date(concat(year(now()) , '-' ,month(now()) , '-' , day(last_day(now()))))
What about this?
SELECT * FROM table
WHERE MONTH(ORDERDATE) = MONTH(NOW() - INTERVAL 1 MONTH) and YEAR(ORDERDATE) = YEAR(NOW() - INTERVAL 1 MONTH)
This should only return rows where the order date is in the previous month.

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: receive data only per months

Few times ago, I asked how to do to display data per month, I must told a bad explanation because I just figured out that it's not what I want :
Here's what I got :
$req1 = ...
AND v.date > (DATE_SUB(CURDATE(), INTERVAL 2 MONTH))
AND v.date < (DATE_SUB(CURDATE(), INTERVAL 1 MONTH))
$req2= ...
AND v.date > (DATE_SUB(CURDATE(), INTERVAL 3 MONTH))
AND v.date < (DATE_SUB(CURDATE(), INTERVAL 2 MONTH))
But the problem, imagine that today you are the 10th June, it's going to calculate ALL the data between the
10 june to the 10 may
then the 10 may until the 10 april...
But what I want is data :
from 1st may to 1 st june,
from 1st june to 1st july...
Do you see what I mean ?
You could use:
WHERE YEAR(date) = 2010 AND MONTH(date) = 5
to get all rows where date is in the YEAR 2010 and the fifth month of the year.
AND MONTH(v.date)=6 AND YEAR(v.date)=2009 [to get everything in June 2009]
select month(v.date), year(v.date), sum(somedatacolumn) from thetable group by month(v.date), year(v.date);
replace sum with whatever calculation you are doing