Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return all mySQL Records for Current Year

Tags:

mysql

In mySQL, I would like to return a count of the records that have a start_date within the current year (as in the year 2012, NOT within the last one year time period) and (as a separate query) a count of the records from the previous year (2011 in the case as it is now).

My start dates are stored thus: 2012-12-02. I am using PHP.

Help on how I would form these queries would be hugely appreciated.

like image 274
user1857692 Avatar asked Nov 27 '12 19:11

user1857692


People also ask

How do I get this year data in MySQL?

Use the YEAR() function to retrieve the year value from a date/datetime/timestamp column in MySQL. This function takes only one argument – a date or date and time. This can be the name of a date/datetime/timestamp column or an expression returning one of those data types.

How do I get current year and next year in SQL?

Just run these SQL queries one by one to get the specific element of your current date/time: Current year: SELECT date_part('year', (SELECT current_timestamp)); Current month: SELECT date_part('month', (SELECT current_timestamp)); Current day: SELECT date_part('day', (SELECT current_timestamp));

Which returns current date in MySQL?

The CURDATE() function returns the current date. Note: The date is returned as "YYYY-MM-DD" (string) or as YYYYMMDD (numeric). Note: This function equals the CURRENT_DATE() function.


1 Answers

SELECT COUNT(*)
 FROM TABLE1
 WHERE YEAR(START_DATE) = YEAR(CURDATE()); 
like image 137
pixeline Avatar answered Sep 29 '22 19:09

pixeline