Solved

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

Posted on 2014-12-15
1
351 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (http://dev.mysql.com/doc/refm…
More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 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