How to order a Mysql search with date in date format to get only the month

Asked

Viewed 104 times

0

I have a field date, would like to make a query to take only the month of this field. How can I do that?

1 answer

1


Just use the function MONTH of MySQL.

SELECT MONTH(seu_campo);
  • Well this... But my problem is not this, I want to make a variable in php that receives this value and sort by this variable... understand?

  • Make your query and at the end put the following ODER BY MONTH(seu_campo).

  • It doesn’t solve... wanted something but rather $query = "SELECT * FROM table ORDER BY MONTH(field, $variable)". Understand?

  • So what you want is to list only the lines in that field date corresponds to the month of its variable in PHP?

  • That buddy... got to do it?

  • You can simplify the operation by doing the processing in select and using an internal query variable for the clauses WHERE and ORDER: SELECT MONTH(date) AS mes FROM table WHERE mes = '$variable' ORDER BY mes

  • So your query would look like this SELECT * FROM tabela WHERE MONTH(seu_campo) = $variavel.

Show 2 more comments

Browser other questions tagged

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