简体   繁体   中英

count votes for specific reply

I have a table that keeps record of vote

Condition

  1. Reply can have many votes;

  2. 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

eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM