MYSQL: How to make a month closure from day 26 to day 26

Asked

Viewed 99 times

0

I have a service registration form, and at the end of the month I have to analyze the closing costs of the amount of services, and pay the service providers.

But the closing of the month is every day 26, so how could I present only the services of the day (27 JANUARY --- until --- 26 FEBRUARY)?

follows the code below: BANCO DE DADOS

<?php
  $soma_custos = mysqli_query($con, "SELECT SUM(custosgerais) FROM custos WHERE MONTH(data_baixa) BETWEEN $mes AND $mes");  
?>

But the above code for now is only showing me the services from day 1 to day 30 of the same month.

I need to pick up from the 27th of last month until the 26th of the current month.

  • Welcome Gabriel Pires, if any answer has solved your problem be sure to mark it as aceita see how and why in this post https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

2 answers

4

Just specify the day and so pick everything that is between the dates:

SELECT SUM(custosgerais) 
FROM custos 
WHERE data_baixa >= '2017-01-26 00:00:00' AND data_baixa < '2017-02-26 00:00:00' 

You can also use the BETWEEN, if you prefer:

SELECT SUM(custosgerais) 
FROM custos 
WHERE (data_baixa BETWEEN '2017-01-26 00:00:00' AND '2017-02-25 23:59:59')

0

The question "I need to pick it up on the 27th of last month until the 26th of current month."

The Where clause in this case is:

WHERE data_baixa >= DATE_FORMAT(CURRENT_DATE - INTERVAL 1 MONTH, '%Y-%m-27')
AND data_baixa <= DATE_FORMAT(NOW() ,'%Y-%m-27')

In the example table below, what you want is the sum of the column custosgerais of the lines selected in blue, that is, between the 27th of last month and the 26th of this month.

inserir a descrição da imagem aqui

Doing the select will return the sum = 122

inserir a descrição da imagem aqui

So, just run the script on the closing day to get the necessary sum. Running before the closing day will get the sum until that day.

Browser other questions tagged

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