Shoumen Agdm Shoumen Agdm - 5 months ago 6
SQL Question

How do I find all data from a table in a specific month

I am trying to retrieve all data from table in a specific month, my date format in the database table is

2016-06-19
I am need to find with the respect of a selected date, like if I select
2016-05-22

it will retrieve
2016-05-07
to
2016-06-06
interval values from the table.

And also to all the values in this month which is
05
month, I am using
mysqli
with PHP. I have tried using this query

SELECT * FROM t_tenancy_details WHERE
agreement_date >= DATE_FORMAT( CURRENT_DATE - INTERVAL 1 MONTH, '%Y/%m/01' )AND
agreement_date < DATE_FORMAT( CURRENT_DATE, '%Y/%m/01' )


This works fine for the running month, But problem is that if I select a previous month it does not work.

Answer

It looks you are asking for a real example try it

SELECT * FROM t_tenancy_details where MONTH(date) = MONTH('2016-05-01')
AND YEAR(date) = YEAR('2016-05-01')

This will select automatically 05 month form the date like also year too. and simply you can also make this input change as your wish.