1
Good I have the following query:
SELECT
SUM(a.valor - a.desconto - a.credito + a.frete)
FROM
pedidos a,
cadastro b
WHERE
a.status = '2'
AND a.data >= '2019-03-29'
AND a.data <= '2019-04-29'
AND a.id_cliente = b.id
AND b.id_cidade = '2378'
It returns me the value of 10.859,95. This value is correct. However I need to recover some more data and to do so assemble this query:
SELECT
SUM(a.valor + a.frete)
FROM
pedidos a,
cadastro b,
produtos_pedidos c
WHERE
a.status = '2'
AND a.data >= '2019-03-29'
AND a.data <= '2019-04-29'
AND a.id_cliente = b.id
AND b.id_cidade = '2378'
AND a.id = c.id_pedido
AND c.grupo = '234'
The problem is that it returns me a much higher value, so I realized the error. The requests are being duplicated, because in a request I have several produtos_pedidos
.
How do I make the table SUM requests but without repeating the ID?
It would be interesting to use aliases more suggestive than
a
,b
,c
. For examplep
forpedidos
,c
forcadastro
andpp
forprodutos_pedidos
– Ronaldo Araújo Alves