Query per month and year (Php + SQL)

Asked

Viewed 1,597 times

1

I have a system in PHP where I use the datatables plugin, I can sort by date, but the user wants to stipulate the period for consultation, for example:

Select from 01/01/2016 until 01/31/2016 and all data from this period will be 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 this case?

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

inserir a descrição da imagem aqui

2 answers

3

The code below will return all records that are between this date range

SELECT suascolunas
FROM sau tabela
WHERE colunaData BETWEEN dataInicio AND dataFinal; 
  • I know this query, the question is how to apply it in the system. Surely I will create two date type fields for the user to select the period you want, after that I send the information typed for this query? Would that be it? SELECT suascolunas

 FROM sau tabela

 WHERE colunaData BETWEEN $dataInicio AND $dataFinal;

  • I just saw the message. That’s right, it’s correct. Just remember that in sql the date needs to be like this > 'Y-m-d'

1

Mysql has 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 made an example to search for a list of birthdays of the month of June.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.