PHP/Mysql Select total balance of 1 client in the table

Asked

Viewed 212 times

1

My client JOSÉ appears 4 times in this table, and I wanted to take the total of his balance. How do I do this?

tabela "clientes"
ID    NOME    SALDO
1     josé     10
1     josé     20
1     josé     30
4     maria    50
5     marta    60
1     josé     100
7     ana      70
8     sara     80

Total de JOSÉ: 160

I wish it were such a simple consultation:

$sql = mysql_query("SELECT user_login FROM clientes ????????? WHERE ID = 1");
  • Just for the record, ID = 'josé' would not bring anything because "jose" is the nome; id is an integer :p

  • Thanks for the remark, I really missed, the correct would be ID = 1

  • Repeated Ids? If this is autoincrement it’s impossible and your table doesn’t make sense. Tell me what you’re doing with JOIN?

  • This part of Ids was a typo, please disregard.

1 answer

1


Use the "sum" function to sum values of a given field:

$sql = mysql_query("SELECT sum(saldo) FROM clientes WHERE ID = 1");

Edit:

If the "ID" field contains a primary key (PK), you should search by name:

$sql = mysql_query("SELECT sum(saldo) FROM clientes WHERE NOME = 'josé'");

Browser other questions tagged

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