Count only on unrepeated data

Asked

Viewed 32 times

0

Consider the following fictitious tables:

Venda = id_venda, data, id_carrinho

Carinho = id_carrinho

ItemCarrinho = id_item, id_carrinho, id_produto

Suppose there is 1 registration for sale, 1 registration in cart and 2 records in itemCarrinho.

If I do this select down below:

select count(b.id_carrinho) as qtd from Venda as a, Carrinho as b, 
ItemCarrinho as c where a.id_venda = 1 and b.id_carrinho = 
a.id_carrinho and c.id_carrinho = b.id_carrinho

it returns me counted in Qtd the value 2, and the correct one would be 1. How to fix this?

  • 1

    Possible duplicate of 2 Groupby in a Mysql query

  • Maybe you can leave the junction with the table Itemcarrinho but, if the logic of your application does not allow, you can also use: COUNT(DISTINCT b.id_carrinho).

No answers

Browser other questions tagged

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