Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had a similar problem but was having this "user-03" kind of row repeating multiple times. And also I was using two tables. </p> <p>Structure of tables:</p> <p>Score Table[score float,uid int,timestamp datetime] User Table[username varchar(500), id int, etc...]</p> <p>The criteria for ranking was to list users based on maximum scores. So I have modified the query using <strong>group by</strong> and <strong>JOIN</strong> on the sql query as given below.</p> <p>Ranking System.</p> <pre><code>SELECT *, @rownum:=@rownum+1 as rank FROM ( select MAX(s.score) as score,u.username as username from scorelog s join userdb u on s.uid=u.id group by s.uid order by MAX(s.score) desc ) user_rank, (SELECT @rownum:=0) r </code></pre> <p>To Identify individual row's position and it's rank on the db use the below query. The </p> <pre><code>SELECT * FROM ( SELECT *, @rownum:=@rownum+1 as rank FROM ( select MAX(s.score) as score, u.username as username, s.uid as uid,s. timestamp as timestamp from scorelog s join userdb u on s.uid=u.id group by s.uid order by MAX(s.score) desc ) user_rank, (SELECT @rownum:=0) r )t where username = 'johndoe' </code></pre> <p>Also refer the below resources from stackoverflow which give similar solution.</p> <p><a href="https://stackoverflow.com/questions/3111290/querying-for-users-ranking-in-one-to-many-tables">querying for user&#39;s ranking in one-to-many tables</a></p> <p><a href="https://stackoverflow.com/questions/1043340/sql-ranking-solution">SQL ranking solution</a></p> <p><a href="https://stackoverflow.com/questions/4567871/php-mysql-query-result-ranking">PHP MYSQL query result &quot;RANKING&quot;</a></p>
    singulars
    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.
    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