Ryan
Ryan

Reputation: 989

MySQL to get previous year record

I want a MySQL query to fetch previous year records. I already wrote a query to fetch current year records but I want previous year record also. There is a column called "date_created" based upon this date I have to fetch the status of the meterial.

SELECT material_status, COUNT(*) c
FROM purchase_order
WHERE YEAR(date_created) = YEAR(CURDATE()) AND material_status='open'; 

Upvotes: 12

Views: 32039

Answers (2)

Mark Dominguez
Mark Dominguez

Reputation: 1

Query to see how the date time functions for MySQL works

Try This:

select 
CURRENT_USER() user, 
CURRENT_TIME() current_time, 
CURRENT_DATE() current_date, 
year(CURRENT_DATE()) current_year, 
YEAR(DATE_SUB(CURDATE(), INTERVAL 1 YEAR)) previous_year;

Upvotes: -2

mohan111
mohan111

Reputation: 8865

to get last year data

SELECT material_status, COUNT(*) c
FROM purchase_order
WHERE YEAR(date_created) = YEAR(DATE_SUB(CURDATE(), INTERVAL 1 YEAR)) AND material_status='open'; 

Upvotes: 22

Related Questions