Nico
Nico

Reputation: 161

Cumulate values for each user

I have a MySQL table with the following structure:

| id | date                 | type | user | approved |
======================================================
| 1  | 2015-01-30 20:32:01  | 2    | 1    | 0        |
| 2  | 2015-01-31 19:40:12  | 1    | 2    | 1        |
| 3  | 2015-02-01 11:12:08  | 2    | 1    | 0        |
| 4  | 2015-02-01 11:32:13  | 4    | 3    | 1        |
| 5  | 2015-02-01 17:25:22  | 6    | 2    | 1        |

I now would like to cumulate the times a task was approved (1) for each user. This is what the result should be:

user 1 --> 0
user 2 --> 2
user 3 --> 1

Is there a way to accomplish this by a single SQL query or would I need to do this outside of MySQL in my php script?

Upvotes: 1

Views: 34

Answers (1)

Iłya Bursov
Iłya Bursov

Reputation: 24146

you can just group and summarize values:

select user, sum(approved)
from table
group by user

Upvotes: 3

Related Questions