How to add the values counted by count mysql?

0

I'm not realizing how to make a query and that's why I come to ask. I have the Users table in which I have the following columns:

id, nombre, numero1, numero2, email

And the following query:

SELECT nombre, count(nombre) FROM Usuarios GROUP BY nombre HAVING count(nombre)>2

What I need is for the query to select rows where the nombre repeats more than 2 times and also add the values of ( numero1 * numero2 ) of each row, giving a final result to be able to order the users with nombre repeated in descending order, how could you do that? Is it possible in a single query? I hope you have explained me well.

Thank you very much

    
asked by Osrion 16.05.2017 в 03:48
source

1 answer

1

To do the sum, you only need to do SUM(numero1 * numero2) , and then you can sort the rows by that result:

select nombre,
       count(*) as cnt,
       sum(numero1 * numero2) as valor_total
  from Usuarios
 group by nombre
 having count(*) > 2
 order by valor_total desc
    
answered by 16.05.2017 / 05:20
source