Relate and Compare MySQL and VB6

1

They passed me a system of a company to update, they have 3 MySQL tables 'vendaesc, venda, venda_forma_payagem'. From what I understand the vendaesc table is used to store the sales items.

I need to create a report that brings me the data of the product sold, quantity sold, unit value, total value of the sale, form of payment, parcels and the problem comes there, often in a single sale insert numerous products, there is a single form of payment for sale. The form of payment information is saved in the Sale table.

The structure of the tables: Sale

'id' int(11) NOT NULL AUTO_INCREMENT,
'Cod' varchar(10) NOT NULL DEFAULT '',
'Data' date DEFAULT NULL,
'Hora' varchar(50) NOT NULL DEFAULT '',
'Desconto' varchar(20) NOT NULL DEFAULT '',
'TotalS' double(10,2) NOT NULL,
'TotalC' double(10,2) NOT NULL,
'Cod_Cliente' varchar(20) NOT NULL DEFAULT '',
'Nome_Cliente' varchar(150) NOT NULL DEFAULT '',
'Mes' varchar(50) NOT NULL DEFAULT '',
'Ano' varchar(4) NOT NULL DEFAULT '',
'forma_pagamento' int(11) DEFAULT NULL,
'forma_pagamento_detalhe' int(11) DEFAULT NULL,
'forma_pagamento_detalhe_nome' varchar(40) NOT NULL DEFAULT '',
'forma_pagamento_parcelas' int(11) DEFAULT NULL,
PRIMARY KEY ('id')

Vendaesc

'Cod' varchar(20) NOT NULL DEFAULT '',
'Venda' varchar(10) NOT NULL DEFAULT '',
'CodTipo' varchar(10) NOT NULL DEFAULT '',
'Tipo' varchar(50) NOT NULL DEFAULT '',
'ObsTipo' varchar(50) NOT NULL DEFAULT '',
'CodProd' varchar(20) NOT NULL DEFAULT '',
'Prod' varchar(50) NOT NULL DEFAULT '',
'referencia' varchar(50) NOT NULL DEFAULT '',
'ValUniProd' varchar(15) NOT NULL DEFAULT '',
'UniProd' varchar(5) NOT NULL DEFAULT '',
'QuantidadeProd' varchar(10) NOT NULL DEFAULT '',
'ValorProd' varchar(20) NOT NULL DEFAULT '',
'Maquina' varchar(50) NOT NULL DEFAULT '',
'Cod_Cliente' varchar(50) NOT NULL DEFAULT '',
'Cliente' varchar(250) NOT NULL DEFAULT '',
'Data' date DEFAULT NULL,
'Mes' varchar(45) NOT NULL DEFAULT '',
'Ano' varchar(4) NOT NULL DEFAULT '',
'Cod_vendedor' varchar(15) NOT NULL DEFAULT '',
'Nome_Vendedor' varchar(155) NOT NULL DEFAULT '',
PRIMARY KEY ('Cod')

Payment Form

'cod' varchar(20) NOT NULL DEFAULT '',
'descricao' varchar(255) NOT NULL DEFAULT '',

Sql I tried, 20% of records returned with wrong data compared to equal or different

SQL = "SELECT *,
IF(venda.cod = vendaesc.Venda and venda.forma_pagamento > 1,'IGUAL','DIFERENTE') AS resultado_agrupa,
venda.TotalS AS total_venda_agrupa,
venda_forma_pagamento.descricao AS nome_forma_pagamento,
vendaesc.QUANTIDADEprod as qtd, 
(vendaesc.QUANTIDADEprod * vendaesc.ValUniProd) As total_soma

FROM vendaesc,venda,venda_forma_pagamento

where vendaesc.data='" & DateRat & "' AND
venda.cod = vendaesc.venda AND venda.forma_pagamento =
venda_forma_pagamento.cod  order by venda.cod"

Programming is VB6: (

SQL:

SQLFiddle

    
asked by anonymous 02.12.2015 / 21:07

2 answers

1

Reading your question and especially this comment:

  

Oops, the idea would be to generate a report listing all   sale, as there may be several items in vendaesc for a   I need the report to inform me that these certain items   part of the same sale, showing to the user if it is of the   same sell the word EQUAL, if not, DIFFERENT

And I would say that you are trying to do the wrong thing, in your SELECT you try to put a IF it will compare the value of column 1 with column 2 of the same row. In your case it returns different only when the form of payment is greater than one ( venda.forma_pagamento > 1 ). Check it out you'll see that's why. I say this because if you make JOIN in venda and vendaesc by cod equal venda ( AND venda.cod = vendaesc.venda ) and in SELECT you put IF(venda.cod = vendaesc.Venda then this IF will always be true, because if it were false it would not appear, because WHERE is obliging to come only those that are true. Got it?

With the SQL below you can see the column cod with column venda always equal, is the same WHERE of yours, but with SELECT changed to make it easier to see the result. Notice the first two columns. FIDDLE

SELECT venda.cod, vendaesc.venda, venda.forma_pagamento, vendaesc.codprod
  FROM vendaesc,
       venda,
       venda_forma_pagamento 
 where vendaesc.data='2015-11-12' 
   AND venda.cod = vendaesc.venda 
   AND venda.forma_pagamento = venda_forma_pagamento.cod  
 order by venda.cod

Cod Venda   forma_pagamento CodProd
000001  000001  1   00002
000002  000002  1   00002
000003  000003  1   00001
000004  000004  4   00001
000004  000004  4   00058
000004  000004  4   00067
000005  000005  1   00001
000005  000005  1   00002

To reinforce let's include your IF in SELECT , but only the first part: FIDDLE

SELECT IF(venda.cod = vendaesc.Venda,'IGUAL','DIFERENTE'),
        venda.cod, vendaesc.venda, venda.forma_pagamento, vendaesc.codprod
  FROM vendaesc,
       venda,
       venda_forma_pagamento 
 where vendaesc.data='2015-11-12' 
   AND venda.cod = vendaesc.venda 
   AND venda.forma_pagamento = venda_forma_pagamento.cod  
 order by venda.cod

IF(venda.cod = vendaesc.Venda,'IGUAL','DIFERENTE')  Cod Venda   forma_pagamento CodProd
IGUAL   000001  000001  1   00002
IGUAL   000002  000002  1   00002
IGUAL   000003  000003  1   00001
IGUAL   000004  000004  4   00067
IGUAL   000004  000004  4   00001
IGUAL   000004  000004  4   00058
IGUAL   000005  000005  1   00002
IGUAL   000005  000005  1   00001

I do not need to say that if you put the second part of IF into SELECT all records that have payment form 1 will come with DIFERENTE result in IF . Do you agree?

What I suggest in your case is, believing you are using crystal report, is to do the following SQL: (The site is saying that I need 10 reputation points to publish more than 2 links, not I understand why, so I will not put the fiddle of that SLQ, but it works on your fiddle anyway, sorry for that)

SELECT vendaesc.*, venda.forma_pagamento
  FROM vendaesc,
       venda,
       venda_forma_pagamento 
 where vendaesc.data='2015-11-12' 
   AND venda.cod = vendaesc.venda 
   AND venda.forma_pagamento = venda_forma_pagamento.cod  
 order by venda.cod

Where it returns all the columns you need with WHERE serving the filters and in the report you create a grouping with the sales code, so all sales items are organized by sales.

The most important thing in this answer is to understand your IF in SELECT , it does not compare two different rows in the result, it is a IF for each row.

PS: change vendaesc.* to a list with all columns, it would be a good practice not to use *

    
19.12.2015 / 17:36
0

I believe that I understand what you want, I would like to display all the records (one on each line) depending on the quantity of products sold, displaying the complete sales information.

If yes, try this query:

SELECT * FROM venda v
LEFT JOIN vendaesc vesc ON v.Cod = vesc.venda
LEFT JOIN venda_forma_pagamento vfp ON vfp.cod = v.forma_pagamento
LEFT JOIN (SELECT Venda, SUM(CAST(REPLACE(ValorProd, ",", ".") AS DECIMAL(10,2))) as TOTALVALORPRDUTO FROM vendaesc GROUP BY Venda) as total ON total.Venda = v.Cod

I would like to point out that it is not a good practice to use VARCHAR to store monetary values, so avoiding FLOAT can also be dangerous. the recommended one is DECIMAL (10,2) for example.

** I had to add a POG to convert the valueprod to decimal and replace comma by point, because in the schema it is like varchar.

    
03.12.2015 / 17:04