Selecting all records from one year ago till now - mysql

I have a table filled with a lot of rows and I need to select all the rows that are less than a year old till now.
The table (called orders) has a DateTime column named order_date, that's the field that determines when the order was placed.
How can I select all the records that have an order_date between now and a full year ago?

select *
from orders
where order_date >= DATE_SUB(NOW(),INTERVAL 1 YEAR);

SELECT * FROM order WHERE order_date >= curdate() - interval 1 year;

To first of month a year ago
SELECT DATE_SUB(DATE_FORMAT(CURRENT_DATE,'%Y-%m-01'),INTERVAL 1 YEAR);

I hope it helps you:
select *
from table
where (order_date BETWEEN '2/15/2011 3:36:18 PM' AND '2/17/2011 9:00:00 PM')

Related

MySQL select records for the past 3 months

what will be the smartest way to select all rows from MySQL table for the past 3 months if the table has the following columns:
| id (int) | year (int)| month (int) |
Considering that if the current month & year are for example 2.2016 I need to select all records for 11.2015 & 12.2015 & 1.2016
It is easy if the current month is greater than 3 because all months that I need to select are in the same year so I can subtract 3 from the current month and run simple query
SELECT * FROM mytabe where year=2016 and month >= xx
Select * from mytable where STR_TO_DATE(concat(year,"-",month,"-01"),'%Y-%m-%d')>date_sub(curdate(),Interval 3 month) ;
The above query will get fetch year and month from date 3 months before today
You can select three Months records by these queries follow this.
The columnName means which column data want you select.
The tableName means which table data want you select.
The dateColumnName means which column date base you want to select data.
It would return Last Month data from today.
SELECT columName FROM tableName WHERE dateColumName BETWEEN ( DATE(NOW()) - INTERVAL 1 MONTH) AND Date(Now())
It would return Second Last Month data from today.
SELECT columName FROM tableName WHERE dateColumName BETWEEN ( DATE(NOW()) - INTERVAL 2 MONTH) AND ( DATE(NOW()) - INTERVAL 1 MONTH)
It would return Third Last Month data from today.
SELECT columName FROM tableName WHERE dateColumName BETWEEN ( DATE(NOW()) - INTERVAL 3 MONTH) AND ( DATE(NOW()) - INTERVAL 2 MONTH)
May It help to others.
Please try this
Select * from mytable where STR_TO_DATE(concat(year,"-",month,"-01"),'%Y-%m-%d')>date_sub(curdate(),Interval 3 month) ;

Query MySQL Datetime Field For Any Records Made 11 Months Ago

Essentially, I have an orders table and each day I'm running a cronjob to check the logs and see which customers ordered 11 months ago, because I need to warn them that their warranty expires in 1 month. What's the best way this query is accomplished?
I could do it a few ways, but I'm interested in seeing opinions of the best way.
SELECT *
FROM orders
WHERE order_date = CURDATE() - INTERVAL 11 MONTH
;
This will find all rows with order_date value exactly 11 months old.
I'd use date_add with a month interval and compare it to now():
SELECT *
FROM orders
WHERE DATE_ADD (order_date, INTERVAL 11 MONTH) <= NOW()
SELECT *
FROM orders
WHERE order_date = UNIX_TIMESTAMP(DATE_SUB(curdate(), INTERVAL 11 MONTH))
I have added UNIX_TIMESTAMP as MySQL stores date in UNIX timestamps

How to get last year's date range?

I would like to get a date range between LastYear/1/1 until LastYear/12/31
I know I could do this
date_sub(now(), interval 1 year). But this would get me 2013/03/08. Not sure how to change the day and the month.
SELECT *
FROM orders
WHERE dispatch_date between `LastYear/1/1` AND `LastYear/12/31`
You can easy to create the required dates:
SELECT *
FROM orders
WHERE dispatch_date >= MAKEDATE(YEAR(NOW()) - 1, 1) -- first day of previous year
AND dispatch_date < MAKEDATE(YEAR(NOW()), 1) -- first day of current year
I would suggest you to use YEAR().
SET #LastYear = YEAR(DATE_SUB(NOW(),INTERVAL 1 YEAR));
SELECT *
FROM orders
WHERE dispatch_date
BETWEEN CONCAT(#LastYear,'-01-01') AND CONCAT(#LastYear,'-12-31')

Get result if end_date is 1 month before current month

In the sales table I have a field called end_date (date type),
I want to display the result if the end_date is current month.
also If the end_date is future date then dispay the result if end_date is 1 month before current month.
How to get that to work in SQL query?
For example like that:
select *
from sales
where DATE_FORMAT(date_end, '%Y%m') = DATE_FORMAT(now() - interval 1 month, '%Y%m')
Returns all rows where end_date is current month or any day earlier (after your comment: "Also if end_date like 4 months ago, it should display as well." – user791022):
select * from sales where end_date < adddate(last_day(now()), 1)
ps. I don't have a MySQL right now so I can't test it.

get all months last days in mysql

For example I have a table with fields:
id date
1 2001-01-01
2 2001-01-05
.................
N 2011-12-31
How get i get all months last days from this table?
example:
if i have dates like 2001-05-31 and 2001-06-01
i need only 2001-05-31 not both
You can do SELECT LAST_DAY for example the below returns Oct. 31st. 2010
SELECT LAST_DAY('2010-10-10');
select max(date_format(date, '%d')) as last_day_of_the_month
from table
group by date_format(date, '%Y%m')
maybe this would work better?
select DISTINCT(LAST_DAY(date)) from table GROUP BY date_format(date, '%Y%m')
SELECT id, date
FROM `table`
WHERE DATE( date ) = LAST_DAY( date )
The DATE function over field is for filter the date without time, use only if you have a datetime column.
The query get all rows with date = last day of month.
select subdate(adddate(subdate(`date`, day(`date`) - 1), interval 1 month), 1)
Note: This is the "hard way". See #harper89's answer - it's better :)
I found a solution. but this query is very slow on large tables. so I am still looking for a better solution
select DISTINCT(LAST_DAY(date)) from table;
select max(date)
from table
group by year(date), month(date)