简体   繁体   中英

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

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

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