filter records by month

Asked

Viewed 638 times

5

I have the following appointment:

$sql= mysql_query("SELECT dia, GROUP_CONCAT(hora) FROM marcacoes WHERE colaborador_id = {$colaborador_id} GROUP BY dia ");

Returns the following table:
inserir a descrição da imagem aqui

but I want it to appear only in the month 6 for example, it appears every month.

How do I display the records I have in the bank per month?

  • Just one observation, don’t use "mysql_query" anymore, because it’s getting obsolete. Use "mysqli_query", which in addition to the query will ask you for the connection as parameter too

  • you have a field with full date?

  • have yes, date type (0000-00-00)

  • In your query interested year? or query can return records from July 2015, 2014 etc?

  • 1

    Smoke Rohden, consider using mysqli as I told you, so that in the future you will not have problems

  • I use the year too, but I already got here thanks very much helped!

  • 2

    Dichrist I will consider yes, thanks for the tip and thank you very much

Show 2 more comments

1 answer

6


It is necessary to filter by month and year, take the field containing the date apply a month() which return the date month and year() that returns the year and makes the comparison with the desired value. The query should be more or less like this:

SELECT dia, GROUP_CONCAT(hora) FROM marcacoes
WHERE colaborador_id = {$colaborador_id} AND month(campo_data) = 6
AND year(campo_data) = 2016 GROUP BY dia
  • rray, I may be mistaken, but this condition of the year 2016 should be in the query? because he wants to make a report of every month of June, comparing year to year. This can be very useful for other situations such as seasonality of products for example.

  • @Dichrist the code to be used for labor point tidal, if not put the year it will take the records of previous years, I imagine he wants only the current year. I’ll confirm that.

  • Yes, it also appears to me to be check-in time, round trip for lunch and check-out. But as I wasn’t sure, I made this observation.

Browser other questions tagged

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