Mysql extract from INPUT and OUTPUT tables

Asked

Viewed 250 times

0

I’m a beginner in Mysql and PHP, I’m building a financial management application for a charity, (volunteer work), and I can’t solve this problem:

Performing a Mysql query in the IN and OUT tables that returns them as a bank statement.

From now on my gratitude for your help. God Bless you!!

Follow the tables and expected result:

01 - ENTRY TABLE

CREATE TABLE IF NOT EXISTS `entradas` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `data` date DEFAULT NULL,
  `mes_referencia` varchar(50) DEFAULT NULL,
  `conta` varchar(255) DEFAULT NULL,
  `doador` varchar(255) DEFAULT NULL,
  `valor_entrada` decimal(32,2) DEFAULT NULL,
  `n_documento` varchar(300) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2465 DEFAULT CHARSET=latin1;**

02 - EXIT TABLE

CREATE TABLE IF NOT EXISTS `saidas` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `data` date DEFAULT NULL,
  `mes_referencia` varchar(50) DEFAULT NULL,
  `valor_saida` decimal(10,2) DEFAULT NULL,
  `conta` varchar(100) DEFAULT NULL,
  `favorecido` varchar(100) DEFAULT NULL,
  `cpf` varchar(30) DEFAULT NULL,
  `categoria` varchar(100) DEFAULT NULL,
  `numero_doc` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1079 DEFAULT CHARSET=latin1;

03 - RESULTS OF MY DREAMS (kkkk) inserir a descrição da imagem aqui

  • 1

    tips: store the previous balance and the current balance in the movement table... and... precisely, make a table only of "moves" does not need two...

1 answer

1


There are some problems in this model, it may be that it’s just a conceptual exercise, it wouldn’t use it in a real problem. Surely you will have problems with retroactive movements or chargebacks.

When you separate it into two tables and the expected result will be just one, you add unnecessary complexity, needing to use UNION in the syntax, and this worsens even more when you need to access the same data several times, as is also necessary to calculate balances.

Note that the structure of the records is the same, yet placed different names in the [donor/favored] columns, everything is about a person relative to the movement. This also happens with the value column, is Placed [input value / output value_value] and it is the same data, only value; Still has the use of the +- signal to indicate if it is an input or output, which also facilitates, if the values are flagged, just add all.

Using the presented structure, I made the following Query to extract the desired data:

select 
max(data) as data, 
null as documento, 
'Saldo Anterior' as conta,
null as pessoa,
0 as entrada,
0 as saida,
sum(valor) as saldo
from
(select data, valor from entradas where data < '2020-01-01'
union all
select data, valor from saidas where data < '2020-01-01') x

union all

select 
data, documento, conta,pessoa,entrada,saidas, entradas_anteriores - saidas_anteriores + entrada - saidas as saldo
from (
  select 
    id, data, documento, conta, pessoa, valor as entrada, 0     as saidas, (select coalesce(sum(x.valor),0) from entradas x where x.data < e.data) as entradas_anteriores, (select coalesce(sum(x.valor),0) from saidas   x where x.data < e.data) as saidas_anteriores  from entradas e where e.data >= '2020-01-01'

     union all
  select 
    id, data, documento, conta, pessoa, 0     as entrada, valor as saidas, (select coalesce(sum(x.valor),0) from entradas x where x.data < s.data) as entradas_anteriores, (select coalesce(sum(x.valor),0) from saidas   x where x.data < s.data) as saidas_anteriores  from saidas   s where s.data >= '2020-01-01'
) m

order by data;

You can see running on Sqlfiddle

Upshot:

inserir a descrição da imagem aqui

Now, using the following structure:

CREATE TABLE IF NOT EXISTS `movimentos` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `data` date DEFAULT NULL,
  `conta` varchar(255) DEFAULT NULL,
  `valor` decimal(32,2) DEFAULT NULL,
  `pessoa` varchar(255) DEFAULT NULL,
  `documento` varchar(300) DEFAULT NULL,
  `saldo_anterior` decimal(32,2) DEFAULT NULL,
  `saldo_atual` decimal(32,2) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2465 DEFAULT CHARSET=latin1;

With the following query I can have the same result:

select 
x.data,
null as documento,
'Saldo Anterior' as conta,
null as pessoa,
0 as valor,
x.saldo_atual
from movimentos x
where x.data < '2020-01-01'

union all

select 
m.data,
m.documento,
m.conta,
m.pessoa,
m.valor,
m.saldo_atual
from movimentos m
where m.data >= '2020-01-01'

Simpler not!?

Follows the Sqlfiddle

Upshot:

inserir a descrição da imagem aqui

Remarks:

I would not put to display the previous balance as a result row, possibly it would be displayed at another location on the screen, as well as the total sum.

In this example there is still problem for the case of retroactive movement.

  • Hello, dear friend @Rovann Linhalis My thank you very much for your generosity in taking some of your precious time to help me. Indeed your explanation helped me a lot! as I already have a system running where there are several records in 2 tables. I decided to use your first example. OK, it worked, but I still have a problem; ENTRIES ARE LISTED IN THE QUERY AND REPEAT AS OUTPUTS, OUTPUTS ARE NOT LISTED. Follow here my real code My code is this one&#If you can help me, I thank you very much! Stay with God!

  • @Maggy in the select of payments, you continued using the table of payments... http://sqlfiddle.com/#! 9/72e0aac/8

  • Wow!!! Thanks!! Really, that’s it. solved. thanks again

  • I leave here my congratulations to this wonderful community, and especially to you @Rovann Linhalis for the prompt attention, your explanation was excellent and fundamental to solve my problem. You are a 1000!! God reward you Thank you, Thank you!!

  • @Maggy is welcome and available, but note that there are some rules in the community, such as not using the answer field to comment or to supplement the questions, you can edit the question or use the comment field. If you are in doubt about this too, there is plenty of material in the community and also feel free to ask (at the goal: https://pt.meta.stackoverflow.com/)

  • Okay. Got it. Thank you

Show 1 more comment

Browser other questions tagged

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