I have a table that keeps record of vote
Condition
Reply can have many votes;
User can vote once to specific reply id.
table: reply_vote
+----+-------------+-------------+
| id | reply_id | userID |
+----+-------------+----+--------+
| 1 | 23 | 21 |
| 3 | 33 | 21 |
| 4 | 23 | 25 |
| 5 | 23 | 12 |
I want to display the total votes on each reply
e.g
reply_id 23 has 3 votes with userid (21,25,12)
reply_id 33 has 1 vote with userid (21)
I don't want to fetch all data using while loop in php
up till now I have used below query but it is displaying all the votes on the page which is not what is required
SELECT
reply.id,
reply.reply,
COUNT(reply_vote.id) AS likes
FROM reply
LEFT JOIN reply_vote ON reply_vote.reply_id = reply.id
GROUP BY reply.id
How can I do that?
Try this code:
SELECT reply.id, reply.reply,
COUNT(reply_vote.id) as likes
FROM reply
INNER JOIN reply_vote ON reply_vote.reply_id = reply.id
GROUP BY reply_vote.reply_id