Query in mysql GROUP BY listing and adding at the same time

Asked

Viewed 342 times

0

How I do a query in mysql GROUP BY listing the rows and returning me a sum of a column x

Example

 DB   | id |  nome |  nota  |
      | 01 | Fabio |`  10   |
      | 02 | Fabio |   20   |
      | 03 | Leo   |   25   |
      | 04 | Leo   |   25   |

     $sql = "SELECT * FROM ps GROUP BY nome";
     $resultado = mysql_query($sql) or die ("Erro na consulta");
     while ($linha2 = mysql_fetch_assoc($resultado)) {

     echo $id = $linha2["id_tec"];
     echo $nome = $linha2["name"];
     echo $nota = $linha2["nota"]; // notas ja somadas
     }

     Resultado Desejado
     01 Fabio 20
     02 Lep   50
  • You really need that first id?

  • Hello is not necessary

1 answer

1


Tries:

SELECT nome, SUM(nota) AS `soma` FROM ps GROUP BY nome;

DB:

 id |  nome | nota
----+-------+-------
'1', 'iuri', '10'
'2', 'iuri', '80'
'3', 'diniz', '4'
'4', 'diniz', '10'
'5', 'diniz', '6'
'7', 'gomes', '1'

RESULT:

 nome  | soma
-------+------
'diniz', '20'
'gomes', '1'
'iuri', '90'

Browser other questions tagged

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