How to average records per day in PHP MYSQLI

Asked

Viewed 94 times

-1

Hello

I wanted to know how to count the average users created per day.

For example:

SECOND Average of 3 customers TUESDAY Average of 4 customers FOURTH Average of 3 customers

and per day

LAST SEVEN DAYS We have an average of 5 customers created per day.

I have no idea where to start.

Currently to count the number of customers I use this code:

//Selecionar todos os clientes da tabela
$query = "SELECT * FROM `clientes`";
$result = mysqli_query($con, $query);

//Contar o total de cursos
$total_cursos = mysqli_num_rows($result);

1 answer

0


If you want to select the records of the last 7 days, I suppose your table has a field called "data_registration", you can count the total records and average making a query like this:

SELECT count(*) as total, (count(*) / 7) as media FROM clientes where data_cadastro > (NOW() - INTERVAL 7 DAY)

Regarding the days of the week, you would need to align the concept. Do you want the average of registrations made on every Monday? Here is an example of how to count all the records made on Mondays

SELECT count(*) as total FROM clientes where WEEKDAY(data_cadastro) = 0

Here is the explanation of how this function works.

https://www.w3schools.com/sql/func_mysql_weekday.asp

You could take this result and divide by the amount of Mondays of the period you want to filter.

Here is a post that comments something like this

https://stackoverflow.com/questions/54569956/get-number-of-monday-in-a-rangedate-mysql

  • How would the DATA field be? because I did according to what you said and only results in the number 1

  • thanks in advance for the help

Browser other questions tagged

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