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

0

How do I make 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
    
asked by anonymous 23.08.2015 / 04:07

1 answer

1

Try:

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'
    
23.08.2015 / 04:25