Solved

Mysql, ordering results by the highest score (inner query)

Posted on 2014-12-15
1
334 Views
Last Modified: 2014-12-15
Hi,

I have two tables in a database;

players
- id (int)
- name (varchar)

player_votes
- id (int)
- player_id (int)
- up_vote (tinyint)

The players table contains a list of football players. In the application users can up vote or downvote a player, each up or down vote results in an entry in the player_votes table, and the up_vote field is either 1 or 0.

What I'm trying to do is a return a list of 10 players that have the highest score. Score being the total number of up_votes of a player, divided by the total number of votes.

I've come up with the following query which I think might have done it, but I'm getting an error "check the manual that corresponds to your MySQL server version for the right syntax to use near '/total_votes AS score ORDER BY score ASC' at line 5".

SELECT p.*
          FROM players AS p,
            (SELECT count(pv1.id) FROM player_votes AS pv1 WHERE player_id = p.id) AS total_votes,
            (SELECT sum(pv1.up_vote) FROM player_votes AS pv2 WHERE player_id = p.id) AS up_votes,
            up_votes/total_votes AS score
          ORDER BY score ASC

Open in new window


I suspect I've probably got this query all wrong, and maybe there's even a much easier was of doing the same thing?
0
Comment
Question by:SheppardDigital
1 Comment
 
LVL 33

Accepted Solution

by:
Mike Eghtebas earned 500 total points
ID: 40500475
SELECT p.Id
              , p.Name
              , count(pv1.id)  AS total_votes
              , sum(pv1.up_vote) / count(pv1.id)  AS score
FROM players AS p inner join player_votes AS pv1
            ON pv1.player_id = p.id
Group By Id, Name
Order By sum(pv1.up_vote) / count(pv1.id)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now