tags:

views:

66

answers:

5

how can i get record from table using month/year? I have a table like

Project

Name - varchar
DueDate -datetime
Status -boll

DueDate is project due date, i want record corresponding to month/year,not full date, i mean record for specific month.

How can i do this in mysql

+2  A: 
SELECT Name FROM Table Where MONTH(datetime) = 1;

1 = January.

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_month

RobertPitt
what if date is "1 JAN 2010" and "2 JAN 2009". according to your query it will find both
Salil
Hmmm.. Surely he can understand that he can use YEAR, im not here to give him the answer but to guide him. i gave hem documentation for MONTH() Function, im sure he would look for YEAR(),DAY() and HOUR() :)
RobertPitt
This will not use an index if one exists on the `duedate` column. Todd R's answer is the best choice.
OMG Ponies
He never asked for an indexable column, he required to select all rows that have the particular month, not by creating ranges for dates although i see your point, if he used some function to generate a $min,$max range this would be more appropriate !
RobertPitt
+1  A: 

You can extract the MONTH() and YEAR() for your DueDate.

KennyTM
+1  A: 
SELECT * WHERE MONTH(DueDate) = '5' AND YEAR(DueDate) = '1987';
oezi
+3  A: 

Simply use MONTH() and YEAR():

SELECT * FROM Project WHERE MONTH(DueDate) = 1 AND YEAR(DueDate) = 2010
Keeper
This will not use an index if one exists on the `duedate` column. Todd R's answer is the best choice.
OMG Ponies
+2  A: 

You could use a between statement:

select * from Project 
  where DueDate between '2010-01-01' and '2010-02-01'
Todd R
OMG Ponies