Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>@JHaasie77 has a good approach, use the <code>candidate_no</code> as an orderer (which then comes down to an alpha-numeric sort order) making 9 first when you use descending order. (likewise, C9 would be last if you used ascending order). If there's no further disqualifiers, then that's the easiest route.</p> <p>If you're making the order decision based on another metric, you probably want to change your table around to add some other column(s) you can order by. (Maybe it's by the "home-town hero" vs the "visitor", or by age with youngest matches first--who knows.) But any other detail would need to be added to the database so you could then use it to create a preference in the result.</p> <p>with that said, you can slim down that query a bit by using a <code>GROUP BY</code> instead of union. this also makes it dynamic enough that if you had more candidates in the future, the query would automatically re-adjust. So, your long query now becomes:</p> <pre><code>SELECT sum ,candidate_no ,@curRank := @curRank + 1 AS rank FROM ( SELECT SUM(score) / 5 SUM ,candidate_no FROM SCORE GROUP BY candidate_no ) a , ( SELECT @curRank := 0 ) r ORDER BY sum DESC ,candidate_no DESC </code></pre> <p>Which basically means that for every unique <code>candidate_num</code> it will sum the score and divide by 5. The above also adds the second orderer for <code>candidate_num</code> making <code>C9</code> appears first <a href="http://www.sqlfiddle.com/#!2/9b20b/5/0" rel="nofollow">as seen here</a>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload