SQL hide query parameter in return

Asked

Viewed 304 times

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 the ORDER BY soma DESC that works, I don’t understand why you need them in this case

  • 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

  • It doesn’t work to make order by sum(quantidade) desc?

  • @Carlosrocha this query does not do this, I will remake it the way you want

  • @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

  • @Andersoncarloswoss, that’s right, thank you!

Show 1 more comment

3 answers

0

Jeferson,

You can substitute for this code, which will return all products sold:

....WHERE idProdutos IN(SELECT distinct(idProdutos)FROM pedidos)

Now if you need the best-selling products, the correct thing would be to use the rank you have in the Oracle case, but doing so I think it works:

SELECT prod.idProdutos,
   prod.idCategorias,
   prod.codigo,
   prod.nome,
   prod.data,
   prod.precoUnitario,
   prod.bloqueado,
   prod.lancamento,
   prod.freteGratis,
   prod.oportunidade,
   prod.descricao,
   prod.desconto,
   prod.estoque,
   prod.peso,
 --  ped.soma -- caso queira mostrar a quantidade remova o comentário
FROM produtos as prod, 
(SELECT idProdutos, SUM(quantidade) as soma
    FROM pedidos
    GROUP BY idProdutos
    ORDER BY soma DESC
) ped 
WHERE prod.idProdutos = ped.idProdutos
LIMIT 0,12
order by ped.soma

0

A good way to do this is by using a JOIN with the table you want and group everything from the first table. It could do in its own way also however the LIMIT would have to be in Subquery and not in Main Query

SELECT 
   p.idProdutos, 
   p.idCategorias,  
   p.codigo,   
   p.nome,    
   p.data,  
   p.precoUnitario,  
   p.bloqueado,  
   p.lancamento,   
   p.freteGratis,
   p.oportunidade,
   p.descricao,  
   p.desconto,
   p.estoque, 
   p.peso
FROM produtos p
  JOIN pedidos pe
    ON p.idProdutos = pe.idProdutos
GROUP BY p.idProdutos, p.idCategorias, p.codigo, p.nome, p.data, p.precoUnitario, p.bloqueado, p.lancamento, 
   p.freteGratis, p.oportunidade, p.descricao, p.desconto, p.estoque, p.peso
ORDER BY SUM(pe.quantidade) DESC 
LIMIT 0,12
  • so it does not give because I am using object orientation and in class products does not have a sum attribute. But Thanks, ordering by the sum also worked

  • @Carlosrocha just take out the query attribute, I’ll adjust the query, just test now

0

Grateful to those who helped.

But I thought it made it simpler:

            SELECT 
              idProdutos, 
               idCategorias,  
               codigo,   
               nome,    
               data,  
               precoUnitario,  
               bloqueado,  
               lancamento,   
               freteGratis,
               oportunidade,
               descricao,  
               desconto,
               estoque, 
               peso
            FROM produtos
            WHERE idProdutos IN (
                SELECT idProdutos
                FROM pedidos
                GROUP BY idProdutos
                ORDER BY sum(quantidade) desc                       
            )
            LIMIT 0,12
  • I believe that this way n will work as you want, the LIMIT has to be in your Subquery and not in the main Query

  • Mysql does not accept LIMIT in subquery. And, imagine that the return is 5 id’s ordered by the largest quantities, for example, all returns would be, ids 25, 4, 7, 12. 15, 49, etc... If the main query limit is for example LIMIT 0.3, then only returns of 25, 4 and 7

  • try this http://stackoverflow.com/a/7124492/4166090

Browser other questions tagged

You are not signed in. Login or sign up in order to post.