How to run a code block from a number of specific items recorded in the table?

Asked

Viewed 26 times

0

I didn’t know how to summarize the question, but here’s the thing:
I have a table (SQL) where there are columns nome and data. There with PHP, I want to check the number of the week from the date and perform an action if there is a number x of registered dates for each name.

Example:

NOME  |  DATA
João  |  15/04/2019
Pedro |  15/04/2019
Maria |  15/04/2019
João  |  17/04/2019
Pedro |  23/04/2019
João  |  24/04/2019

I want to generate results like:

João tem 2 registros na semana 16.
Pedro tem 1 registro na semana 16.
Maria tem 1 registro na semana 16.
João tem 1 registro na semana 17.
Pedro tem 1 registro na semana 17.

Any idea how I could do that?

  • 1
  • Make a loop (for, while, etc.), and for each iteration of that loop, make another loop for the incidences of the name in the week.

  • You can do this directly from the database by calculating the week number, as already stated, and the instruction group by SQL to group similar records according to the rule you define. I believe that, with this, you already have enough material to research and try to do on your own. If you cannot, you can return and [Edit] the question by placing the code done and the result obtained.

1 answer

0

Using your example table you could make a query in the database this way:

SELECT count(*) AS qtd, nome, WEEK(`data`) AS semana FROM semanas_tbl GROUP BY semana;

In this case the result of your query would be

qtd |   nome    |   semana
4   |   João    |   15
2   |   Pedro   |   16

Browser other questions tagged

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