ehftwelve
ehftwelve

Reputation: 3147

2 where clauses on same column

I have a table that contains votes that users make on their friends.

It is setup like this:

+------------+------------+------------+------+
| user_id    | friend_id  | contender  | vote | 
+------------+------------+------------+------+
| 1695929578 | 1695529606 | 1695529577 |    1 | 
| 1695929578 | 1695529577 | 1695529606 |    0 |
| 1695929578 | 1695529721 | 1695529578 |    1 |
| 1695929578 | 1695529578 | 1695529721 |    0 |
| 1695929578 | 1695529580 | 1695529606 |    1 |
| 1695929578 | 1695529606 | 1695529580 |    0 |
| 1695929578 | 1695529606 | 1695529578 |    1 |
| 1695929578 | 1695529578 | 1695529606 |    0 |
| 1695929578 | 1695529577 | 1695529721 |    1 |
| 1695929578 | 1695529721 | 1695529577 |    0 |
+------------+------------+------------+------+

Each time a vote is made by the user (user_id) two rows are inserted. One row for the person who got the vote (vote = 1) and that persons contender, and then another row for the person who lost (vote = 0) and their contender.

With one query I want to be able to pass the users friends and get back how many votes are FOR and TOTAL votes for each distinct friend. It would return something like this:

+------------+-------------+
| family_id  | total_votes |
+------------+-------------+
| 1695529439 |           9 |
| 1695529577 |           9 |
| 1695529578 |           6 |
| 1695529580 |           7 |
| 1695529581 |           5 |
| 1695529606 |           7 |
| 1695529721 |           7 |
+------------+-------------+

The only difference is I want an extra column that will contain the votes_for that person so I can compare how many times they were contended to how many times they won.

Any ideas?

Upvotes: 1

Views: 191

Answers (1)

Smandoli
Smandoli

Reputation: 7019

Count() will give you total number of votes. Sum() will give you total of favorable votes. Is there something I'm missing?

Upvotes: 3

Related Questions