Select record between the first day of month X and the last day of month X

Asked

Viewed 59 times

0

How would the Mysql Query be for me to get all records of the selected month (using a select)?

Date field is of type Date.

SELECT * FROM tabel WHERE primeiro-dia-do-mes-x BETWEEN ultimo-dia-do-mes-x ORDER BY data ASC

I select the month and year in an HTML Select field, so on that date I will enter month and year being variable.

Thank you.

  • 1

    and does not take into account the year? will pick dates of a same month in different years, or only have at all times 1 year in the table?

2 answers

1


Brenno, follow a suggestion using the function STR_TO_DATE to convert the parameters month and year from string to date, and the function LAST_DAY to get the last day of the month:

SELECT * FROM tabela
WHERE 
    data BETWEEN 
        STR_TO_DATE( CONCAT('01/', @mes, '/', @ano), '%d/%m/%Y' ) AND
        LAST_DAY( STR_TO_DATE( CONCAT('01/', @mes, '/', @ano), '%d/%m/%Y' ) )
ORDER BY data ASC

I hope it helps

0

If you have a date you can use the function Month and Year mysql.

SELECT * FROM tabela WHERE MONTH(campodata) BETWEEN MesInicial AND MesFinal

Browser other questions tagged

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