0
I have the following appointment:
SELECT idProdutos, SUM(quantidade) as soma
FROM pedidos
GROUP BY idProdutos
ORDER BY soma DESC
That returns me the id and the aggregated sum of the products in the table.
However, I need you to return only the idProdutos
for me to put in an IN clause as below.
This is kind of hide query parameter soma
on the return.
Is there a way to do that in the MySQL
?
$string = "SELECT
idProdutos,
idCategorias,
codigo,
nome,
data,
precoUnitario,
bloqueado,
lancamento,
freteGratis,
oportunidade,
descricao,
desconto,
estoque,
peso
FROM produtos
WHERE idProdutos IN
(
SELECT idProdutos, SUM(quantidade) as soma
FROM pedidos
GROUP BY idProdutos
ORDER BY soma DESC
)
LIMIT 0,12";
just take the
, SUM(quantidade) as soma
and theORDER BY soma DESC
that works, I don’t understand why you need them in this case– Jeferson Almeida
Well, do you understand this consultation? It searches the sum of the quantities of the products in the order table and orders from the best sellers to the least sellers. But I only need the idProducts to handle this query to use in a IN clause of another query that only accepts the idPoducts
– Carlos Rocha
It doesn’t work to make
order by sum(quantidade) desc
?– Woss
@Carlosrocha this query does not do this, I will remake it the way you want
– Jeferson Almeida
@Carlosrocha the result of this query is not what you expect, to work the
limit
had to be in the inside query and not in the outside query– Jeferson Almeida
@Andersoncarloswoss, that’s right, thank you!
– Carlos Rocha