Add values from a column in the table

Asked

Viewed 37 times

-1

I need to select the 3 users with the highest revenue. Before in the table cde_usuario there was a column called scythe, being very simple to make this query, used this sql:

$sql = "SELECT * FROM cde_usuario ORDER BY qtdreceita DESC LIMIT 3";

However, this column has been removed as the table now exists cde_venda_detail, being one of the columns id_client and value. Now I need to add up all the numbers in the column value of all customers with purchases, and in the end select the 3 customers that resulted in the highest value.

I know that to add up the total value of all purchases of a specific user is like this:

$sql = "SELECT SUM(valor) as total FROM cde_venda_detalhe WHERE id_cliente = $idCliente";

But I don’t know how to "unite" these Selects, if anyone can help me thank you.

1 answer

1


Assess if this is what you want:

SELECT id_cliente, SUM(valor) as total FROM cde_venda_detalhe GROUP BY id_cliente ORDER BY total DESC LIMIT 3;
  • Yes, it worked perfectly, thank you!

Browser other questions tagged

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