Add values of the same code

-2

Hello, I have a table that lists the purchase quantity of each product, for example, if the buyer makes 3 purchase orders he repeats the orders and quantities. An example of this is the image below:

Forexample,therefrigerantthathas3requestsfor900,Iwouldliketoaddthistoappear2700understand?Iwouldliketodothisinmysqlselect?OrbyPHP?

I'dratheritwasformysql,tomaintainthenmakeiteasier.ButinPHPIalsoaccept.

Myselect:

$stid="SELECT b.id, b.codigo, a.codacesso, a.seqproduto, a.desccompleta, b.quantidade, b.data_vencimento, b.data_atual, b.observacao, b.usuario, b.estado, b.loja, a.medvdiageral, a.comprador, a.preco
FROM master_datas_b a, master_coletores b 
WHERE b.tipo_acao IS NULL and a.comprador = :comprador and a.nroempresa = :loja and b.loja = :loja and estado = 'Ativo' and b.codigo = a.codacesso AND b.data_vencimento BETWEEN TO_DATE(:data1,'YYYY-MM-DD') AND TO_DATE(:data2,'YYYY-MM-DD') and b.quantidade > 0 ORDER BY b.data_vencimento, b.codigo ASC";
    
asked by anonymous 12.11.2018 / 14:06

2 answers

1

This would be a grouping of records, as well as other SGBD MySql has a clause precisely for this, which is GROUP BY . With it you will be able to group the data by column seqproduto , it would look something like this:

SELECT
    b.id, b.codigo, a.codacesso, a.seqproduto, a.desccompleta, SUM(b.quantidade) AS quantidade,
    b.data_vencimento, b.data_atual, b.observacao, b.usuario, b.estado, b.loja, a.medvdiageral,
    a.comprador, a.preco
FROM master_datas_b a, master_coletores b 
WHERE
    b.tipo_acao IS NULL and a.comprador = :comprador and a.nroempresa = :loja
    and b.loja = :loja and estado = 'Ativo' and b.codigo = a.codacesso
    AND b.data_vencimento BETWEEN TO_DATE(:data1,'YYYY-MM-DD') AND TO_DATE(:data2,'YYYY-MM-DD')
    and b.quantidade > 0
GROUP BY seqproduto
ORDER BY b.data_vencimento, b.codigo ASC

See more about GROUP BY here .

    
12.11.2018 / 14:52
0

I got this way, why are you refusing my question?

SELECT a.seqproduto, a.desccompleta, SUM(b.quantidade) AS QUANTIDADE
FROM master_datas_b a, master_coletores b 
WHERE b.tipo_acao IS NULL and a.comprador = :comprador and a.nroempresa = :loja and b.loja = :loja and estado = 'Ativo' and b.codigo = a.codacesso AND b.data_vencimento BETWEEN TO_DATE(:data1,'YYYY-MM-DD') AND TO_DATE(:data2,'YYYY-MM-DD') and b.quantidade > 0 GROUP BY seqproduto, desccompleta ORDER BY a.seqproduto, a.desccompleta ASC
    
12.11.2018 / 20:15