I have a select where I have to sort it by field 'qtd' The problem is that I have this field different, and I need the products not to be repeated.
The 'a.id' field stores the product code, ie it can not be repeated. Does anyone know how to solve this?
The 'b.qtd' field must be the sum of the two UNION tables.
Follow my select:
SELECT
DISTINCT a.id,
a.unidade,
a.posicao,
a.nome,
a.peso,
sum(b.qtd) quant
FROM
produtos a,
produtos_pedidos b
WHERE
a.id = b.id_produto
and b.id_pedido IN (3,2)
GROUP BY
a.id,
a.unidade,
a.posicao,
a.nome,
a.peso
UNION
SELECT
DISTINCT c.id,
c.unidade,
c.posicao,
c.nome,
c.peso,
sum(d.qtd) quant
FROM
produtos c,
pedidos_barganha d
WHERE
c.id = d.id_produto
and d.id_pedido IN (3,2)
GROUP BY
c.id,
c.unidade,
c.posicao,
c.nome,
c.peso
ORDER BY quant DESC