Pete
Pete

Reputation: 35

MySQL sum with a special condition

So i would like to sum the points from one of my table. My table structure looks like this:

+----+-------+--------+
| id | piece | points |
+----+-------+--------+
|  1 |     3 |   1200 |
|  2 |     1 |    700 |
|  3 |     2 |    950 |
+----+-------+--------+

I would like to sum the points, but where the piece is more than 1 i would like to multiplicate the points with the piece number. I have this code:

SELECT SUM(points) AS points_sum FROM table

But this is only sums the points once. I what i want is understable. I am looking forward to your answers.

Upvotes: 0

Views: 71

Answers (2)

Raging Bull
Raging Bull

Reputation: 18737

Try this:

SELECT SUM(piece*points) as points_sum
FROM TableName

Result:

POINTS_SUM
6200

See result in SQL Fiddle.

Upvotes: 2

TheMP
TheMP

Reputation: 8427

Try this one:

SELECT SUM(points*piece) AS points_sum FROM table

Upvotes: 1

Related Questions