how to use SUM within an ORDER BY (mysql) [closed]

1

I have the following select in mysql:

SELECT DISTINCT
    a.id,
    a.unidade,
    a.posicao,
    a.nome,
    a.peso
FROM
    produtos a,
    produtos_pedidos b
WHERE
    a.id = b.id_produto
    and b.id_pedido IN (13,12,11)
ORDER BY a.nome

Good with it I have the products of the orders 13,12,11 without repeating in alphabetical order. Well the problem and I wanted to put it in order of quantity, I tried to do the following select:

SELECT DISTINCT
    a.id,
    a.unidade,
    a.posicao,
    a.nome,
    a.peso
FROM
    produtos a,
    produtos_pedidos b
WHERE
    a.id = b.id_produto
    and b.id_pedido IN (13,12,11)
ORDER BY SUM(b.qtd)

But it returns only 1 result. Does anyone know what I might be doing wrong.

    
asked by anonymous 03.05.2016 / 21:52

1 answer

1

Try this:

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 (13,12,11)
GROUP BY 
    a.id,
    a.unidade,
    a.posicao,
    a.nome,
    a.peso
ORDER BY
    a.nome
    
03.05.2016 / 21:57