简体   繁体   English

为特定答复数票

[英]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. 用户可以对特定回复ID投票一次。

    table: reply_vote 表格: 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 23拥有3票,用户ID(21,25,12)

reply_id 33 has 1 vote with userid (21) reply_id 33拥有1个具有用户ID的投票(21)

I don't want to fetch all data using while loop in php 我不想使用PHP中的while循环获取所有数据

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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