I need to show you the details of the customer who bought the most

Asked

Viewed 29 times

0

relacionamentos do banco:

This query is giving error:

select c.* from carrinho_de_compras as cc
    inner join clientes as c
        on c.codigo = cc.cod_cliente
    inner join produtos as p
        on p.codigo = cc.cod_produto
    where (select count(produtos.codigo) from produtos)
 
não entendo bem essa lógica das sub-consultas. Alguém pode me explicar?
  • What a mistake this consultation made?

  • ERROR: argument of WHERE must be type Boolean, not type bigint LINE 6: Where (select Count(products.code) from products)

  • where (select count(produtos.codigo) from produtos) can explain to us what you tried to do on that line?

  • list the quantity of products inserted

  • Within the where?

  • yes, I tried to do a sub query there. The plan was to list the amount of products of each customer and then show which customer bought more.

  • The where is to make logical expressions, conditions, can not be used so... would have to be something like where campo < (select ....). That one select should be in the from

  • I understand I’ll change here, thank you

Show 3 more comments

1 answer

0

solved like this:

                  select 
                        C.nome,              
                        count(CC) as total_compras
                    from 
                        carrinho_de_compras as CC, clientes as C
                    where
                        C.codigo = CC.cod_cliente                       
                    group by C.codigo
                    having count(CC) = 
                        (select MAX(
                            (                           
                            select                       
                                count(CC)
                            from 
                                carrinho_de_compras as CC
                            where
                                C.codigo = CC.cod_cliente                               
                            )) 
                        from clientes as C)

This way he lists the names of the customers who most bought products.

I hope it helps someone.

Browser other questions tagged

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