-1

I would like to calculate a summary of row datas in MySQL with PHP. For example, if I'd like to check xyz's sum: 4+1+9+3

I would like to echo the sum.

+------------+-------+-------+-------+-------+
| username   | valu1 | valu2 | valu3 | valu4 |
+------------+-------+-------+-------+-------+
| bozai.akos |     3 |     0 |     0 |     3 |
+------------+-------+-------+-------+-------+
| xzy.xyz    |     4 |     1 |     9 |     3 |
+------------+-------+-------+-------+-------+
| abc.abc    |     3 |     2 |     7 |     5 |
+------------+-------+-------+-------+-------+
GMB
  • 216,147
  • 25
  • 84
  • 135
  • 1
    Does this answer your question? [MySQL Sum() multiple columns](https://stackoverflow.com/questions/22369336/mysql-sum-multiple-columns) – willwrighteng Dec 15 '20 at 22:32

1 Answers1

2

Do you just want an addition?

select t.*,
    valu1 + valu2 + valu3 + valu4 as res
from mytable t

Note that if any of the column has a null value, the addition returns a null value as well. You might want to avoid that, so:

select t.*,
    coalesce(valu1, 0) + coalesce(valu2, 0) + coalesce(valu3, 0) + coalesce(valu4, 0) as res
from mytable t
GMB
  • 216,147
  • 25
  • 84
  • 135