3
I have a select where I have to sort it by the field 'Qtd' the problem is that I have this different field, and I need the products not to repeat.
The 'a. id' field stores the product code, that is, you cannot repeat. Someone knows how to fix this?
The field 'b. Qtd' has to 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
I don’t understand your question. You what to do
distinct
just by the id? Is that it? If yes, you can doselect distinct (a.id), a.unidade, a.posicao....
– igventurelli
That’s right, 'a. id' and 'c.id' cannot repeat themselves, and 'Quant' has to be the total of 'Qtd'. I tried it your way and keep repeating
– Hugo Borges