Solved

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

Posted on 2014-12-15
1
374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 34

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

726 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