Query by month and year (Php + SQL)

1

I have a system in PHP in which I use the datatables plugin, I can sort by date, but the user wants to set the query period, for example:

Select from 01/01/2016 to 01/31/2016 and all data for that period is displayed.

I have to create the fields for the user to select the desired search month and with this I need to create a query for each month of a given year that the user select or there is another way, what to do in that case? >

Example: I would have to do a query for Jan, Feb, Mar, April .. and per year 2015, 2016, 2017 and ...?

    
asked by anonymous 17.06.2016 / 21:24

2 answers

3

The code below will return all records that fall within this date range.

SELECT suascolunas
FROM sau tabela
WHERE colunaData BETWEEN dataInicio AND dataFinal; 
    
17.06.2016 / 21:54
1

In MySQL you have a function called Month that you can use to fetch something according to the month of the year.

An example:

SELECT nome,endereco
FROM clientes
WHERE Month(data-nascimento) = '06'

In the code above I did an example to search for a list of birthdays for the month of June.

    
17.06.2016 / 21:41