Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select mySQL based only on month and year

Tags:

php

mysql

I have a column in my mySQL DB that has some rows. One of this row is a DATE, like this: 2012-02-01

What I want to achieve is to do a SELECT with PHP based only on the year and month.

The logic of the SELECT will be the following:

$q="SELECT * FROM projects WHERE Date="SELECT HERE THE SPECIFIC YEAR AND MONTH""; 

The specific month and year will be be passed from a $_POST variable, like this $_POST['period']="2012-02";

How can I do it?

like image 360
DiegoP. Avatar asked Feb 01 '12 23:02

DiegoP.


People also ask

How do I select a specific month record in SQL?

To select all entries from a particular month in MySQL, use the monthname() or month() function.

How do I select a date range in MySQL?

If you need to select rows from a MySQL database' table in a date range, you need to use a command like this: SELECT * FROM table WHERE date_column >= '2014-01-01' AND date_column <= '2015-01-01';

How can get month and day from date in SQL?

If you use SQL Server, you can use the DAY() or DATEPART() function instead to extract the day of the month from a date. Besides providing the EXTRACT() function, MySQL supports the DAY() function to return the day of the month from a date.


1 Answers

SELECT * FROM projects WHERE YEAR(Date) = 2011 AND MONTH(Date) = 5 
like image 145
Rick Kuipers Avatar answered Oct 09 '22 07:10

Rick Kuipers