简体   繁体   English

使用PHP在MySQL表中找到最高的投票数

[英]using php to find highest number of votes in mysql table

im looking for a way in php to grab entries from a table named 'answers' in a mysql database, then grab entries from a table called 'votes' and calculate from them, the answer with the highest vote. 我在php中寻找一种从mysql数据库中名为“答案”的表中获取条目的方法,然后从名为“ votes”的表中获取条目,并从中计算出投票率最高的答案。 I cant seem to find any help on this. 我似乎找不到任何帮助。

The structures can be viewed here 结构可以在这里查看

How it works is the users vote gets stored in the 'votes' table and is recognized by the 'answer_id' column, i just dont no how count them all and determine the answer with the highest votes 它的工作原理是用户的投票存储在“投票”表中,并由“ answer_id”列识别,我只是不知道如何对他们全部进行计数并确定投票数最高的答案

what about 关于什么

SELECT a.*, COUNT(v.id) tot
FROM answers a INNER JOIN votes v
on a.id = v.answer_id
GROUP BY a.id
ORDER BY tot DESC

If you want to get also answers without any vote, use: 如果您也想不经投票获得答案,请使用:

SELECT a.*, COUNT(v.id) tot
FROM answers a LEFT JOIN votes v
on a.id = v.answer_id
GROUP BY a.id
ORDER BY tot DESC

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

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