Get the month of the mysql datetime field

Asked

Viewed 1,528 times

0

Colleagues,

I have a table where you have the Mysql datetime() field. I would like to take a given month directly from the Where clause. By PHP I know how to do, but how would I do directly by query? I tried this way to get the May records but it didn’t work:

$mês = $_POST["MesBusca"]; // Vamos supor que seja mês de maio (05)
SELECT * FROM tabela WHERE MONTH(DataAbertura) = MONTH(".$mes."); // Tentei também com o 5

inserir a descrição da imagem aqui

  • 2

    That’s the code right there. Maybe not adding the year otherwise it will take all the records of May of all the years registered in the bank. What is the query problem? returns nothing? returns more?

  • Hello rray. Returns nothing. I edited my post with a column print.

  • I don’t quite understand what you want

  • 1

    See my response below.

1 answer

1


Well friend did not understand very well what you want; But let’s see if you want to get all the records for the month of May:

SELECT * FROM tabela WHERE MONTH(DataAbertura) = 5;

Where 5 is the month number, Month() already returns an integer value does not need to convert Month(5) as you did;

Now if you want to get the month of a specific date it would be like this:

select MONTH(DataAbertura) from tabela
  • 1

    Perfect Gabriellocalhost. It was just that. Remove the MONTH(05) and put only the 5. Thank you!

Browser other questions tagged

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