SQL Rank of users score, from arbitrary position - php

Looking around the site, most questions regarding to Ranks in a highscore table assumes that you will be looking at the entire or the top of the table.
In a lot of examples on this site, the rank is found by ordering the items by score and then counting the rows from the top of the set, or counting the items as they are retrived. Like this
score name rank
1000 test345 1
999 test980 2
950 test234 3
833 test291 4
760 test573 5
731 test981 6
In my situation, I need to look at only a portion of the scores, which may not be at the top of the table, for instance, maybe halfway though the leaderboard:
scores name rank
500 test451 43
433 test768 44
425 test120 45
where the user is only shown the scores around his. The part of the leader board the user is looking at above, isn't at the top of the leader board, so I can't count the rows in the returned scores to determine their rank.
How can I determine the rank of user in a leader board at and arbitrary position efficiently, amusing there a lot of entries.
Also this is my first foray into sql and php. I might not be using the correct terminology.

I'm not really sure what you are trying to do. You can limit you result using a LIMIT clause like this:
SELECT * FROM <table> LIMIT 0, 3
Which will only return the first 3 records.
To order the result based on the rank field you would use an ORDER BY clause:
SELECT * FROM <table> ORDER BY rank DESC LIMIT 0, 3
The above query will return 3 records order by rank in descending order.
If you like to calculate the rank based on the scores column this would work:
SELECT scores,
name,
FIND_IN_SET(scores, (SELECT GROUP_CONCAT(scores ORDER BY scores DESC)
FROM <table>)) as rank
FROM <table> ORDER BY rank DESC LIMIT 0, 3;
Running the above query against a table with only two columns scores and name:
+--------+---------+
| scores | name |
+--------+---------+
| 500 | test451 |
| 433 | test768 |
| 425 | test120 |
| 300 | test001 |
| 250 | test002 |
| 200 | test003 |
+--------+---------+
Would yield the following result:
+--------+---------+------+
| scores | name | rank |
+--------+---------+------+
| 500 | test451 | 1 |
| 433 | test768 | 2 |
| 425 | test120 | 3 |
+--------+---------+------+
The GROUP_CONCAT() maximum length is depending on the group_concat_max_len system variable, so for a large table this needs to be changed and I'm not sure this would be the best approach.
Notice that you could/should add indexes to your table to speed things up:
ALTER <table> ADD INDEX `idx_scores` (`scores`);

Related

How to select pagination wise N number of records from MySQL Database?

In one of my recent PHP (CodeIgniter) based projects, a MySQL table contains almost 30.000 records (and will increase more). If I show those records as a list, the performance is reduced (MySQL retrieves all of those records in a quick time, but running a PHP loop for around 30.000 times is not feasible). So my plan is to load 100 records at a time and create a pagination so that the user can select a page number to load that specified number of records.
I can load the first 100 records easily using the following query:
SELECT * FROM <table_name> ORDER BY id ASC LIMIT 100
But the problem is: when I select page=2, page=3, ...., page=n. How can I write a MySQL query that will load 2nd 100 records (for page=2), 3rd 100 records (for page=3),... ... nth 100 record (for page=n)?
N.B: Please refer the 101 number record may not have the id 101, similar 201 number record may not have the id of 201, so please don't suggest me a query that will depend on Primary Key
Can anyone help me to find out the correct query?
Thanks
first hundred
SELECT * FROM <table_name> ORDER BY id ASC LIMIT 0, 100
next hundred
SELECT * FROM <table_name> ORDER BY id ASC LIMIT 100, 100
you're very observant about putting the order by in
THE LIMIT STATEMENT EXPLAINED:
The LIMIT statement is NOT a WHERE clause. It does not select by id nor in fact by any criteria, (there where clause does that) Instead the limit clause simply ensures that you are returned a piece of the block of results that are subset of "everything". Hence why it is important to mention an order by each time, so that each subsequent call will give you the correct piece of the data block in order, and you can 'next', 'next', 'next' through them
EG: for the disordered table this_table:
+-------+-------------+
| id | value |
+-------+-------------+
| 1 | bob |
| 12 | fish |
| 112 | pink |
| 2 | cat |
| 8 | dog |
| 56 | blue |
| 88 | grey |
| 87 | red |
+-------+-------------+
the selects return as below:
SELECT * FROM <this_table> ORDER BY id ASC LIMIT 0,5
+-------+-------------+
| id | value |
+-------+-------------+
| 1 | bob |
| 2 | cat |
| 8 | dog |
| 12 | fish |
| 56 | blue |
+-------+-------------+
and
SELECT * FROM <this_table> ORDER BY id ASC LIMIT 5,5
+-------+-------------+
| id | value |
+-------+-------------+
| 87 | red |
| 88 | grey |
| 112 | pink |
+-------+-------------+
notice the lack of rows 9 and 10 this is deliberate and shows MySQL working as intended
incidentally you should also look at adding an index on id this will MASSIVELY increase the speed of these selects
ALTER TABLE <table_name> ADD INDEX `id` (`id`)
try this
SELECT * FROM <table_name> ORDER BY id ASC LIMIT <p * 100>, 100
so u must have
SELECT * FROM <table_name> ORDER BY id ASC LIMIT 0, 100
SELECT * FROM <table_name> ORDER BY id ASC LIMIT 100, 100
this work if first page = 0, else, u need (p-1) * 100
https://www.w3schools.com/php/php_mysql_select_limit.asp

php/mysql multiple order by

I am working on a results table at the minute, I am wanting to sort a table, by points (highest at the top), if points are equal I want to sort by goal difference, and then if goal difference is equal I want to sort by goals scored.
So a table may look like this,
+--------+--------+----------+-----------------+--------+
| Team | Scored | Conceded | Goal Difference | Points |
+--------+--------+----------+-----------------+--------+
| Team A | 20 | 10 | +10 | 15 |
| Team B | 20 | 15 | +5 | 15 |
| Team C | 10 | 10 | 0 | 9 |
| Team D | 5 | 5 | 0 | 9 |
+--------+--------+----------+-----------------+--------+
So Team A wins the league because it has a better goal difference than Team B, Team C finish above Team D because they score more goals, and all other things are equal.
Is it possible to order this way in mysql, or will I need to parse the results with PHP?
Guess what, you can pass multiple column names to ORDER BY
SELECT * FROM mytable ORDER BY Points DESC, `Goal Difference` DESC, Scored DESC
You haven't given your table structure, but as pointed out by jpg if these fields are not numeric, ORDER by field_name + 0 may be more appropriate
You can do like this using query
SELECT * from `your_table` ORDER BY points DESC, goal_difference DESC, scored DESC
You can order by multiple columns at the same time.
SELECT some_cols
FROM table
WHERE (some conditions)
ORDER BY col1 DESC, col2, col3;

MySQL: GROUP BY within ranges

I have a table with scores like this:
score | user
-------------------
2 | Mark
4 | Alex
3 | John
2 | Elliot
10 | Joe
5 | Dude
The table is gigantic in reality and the real scores goes from 1 to 25.
I need this:
range | counts
-------------------
1-2 | 2
3-4 | 2
5-6 | 1
7-8 | 0
9-10 | 1
I've found some MySQL solutions but they seemed to be pretty complex some of them even suggested UNION but performance is very important. As mentioned, the table is huge.
So I thought why don't you simply have a query like this:
SELECT COUNT(*) as counts FROM score_table GROUP BY score
I get this:
score | counts
-------------------
1 | 0
2 | 2
3 | 1
4 | 1
5 | 1
6 | 0
7 | 0
8 | 0
9 | 0
10 | 1
And then with PHP, sum the count of scores of the specific ranges?
Is this even worse for performance or is there a simple solution that I am missing?
Or you could probaly even make a JavaScript solution...
Your solution:
SELECT score, COUNT(*) as counts
FROM score_table
GROUP BY score
ORDER BY score;
However, this will not returns values of 0 for count. Assuming you have examples for all scores, then the full list of scores is not an issue. You just won't get counts of zero.
You can do what you want with something like:
select (case when score between 1 and 2 then '1-2'
when score between 3 and 4 then '3-4'
. . .
end) as scorerange, count(*) as count
from score_table
group by scorerange
order by min(score);
There is no reason to do additional processing in php. This type of query is quite typical for SQL.
EDIT:
According to the MySQL documentation, you can use a column alias in the group by. Here is the exact quote:
An alias can be used in a query select list to give a column a
different name. You can use the alias in GROUP BY, ORDER BY, or HAVING
clauses to refer to the column:
SELECT
SUM(
CASE
WHEN score between 1 and 2
THEN ...
Honestly, I can't tell you if this is faster than passing "SELECT COUNT(*) as counts FROM score_table GROUP BY score" into PHP and letting PHP handle it...but it add a level of flexibility to your setup. Create a three column table as 'group_ID', 'score','range'. insert values into it to get your groupings right
1,1,1-2
1,2,1-2
1,3,3-4
1,4,3-4
etc...
Join to it on score, group by range. THe addition of the 'group_ID' allows you to set groups...maybe have group 1 break it into groups of two, and let a group_ID = 2 be a 5 set range (or whatever you might want).
I find the table use like this is decently fast, requires little code changing, and can readily be added to if you require additional groupings or if the groupings change (if you do the groupings in code, the entire case section needs to be redone to change the groupings slightly).
How about this:
select concat((score + (1 * (score mod 2)))-1,'-',(score + (1 * (score mod 2)))) as score, count(*) from TBL1 group by (score + (1 * (score mod 2)))
You can see it working in this fiddle: http://sqlfiddle.com/#!2/215839/6
For the input
score | user
-------------------
2 | Mark
4 | Alex
3 | John
2 | Elliot
10 | Joe
5 | Dude
It generates this:
range | counts
-------------------
1-2 | 2
3-4 | 2
5-6 | 1
9-10 | 1
If you want a simple solution which is very powerful, add an extra field within your table and put a value in it for the score so 1 and 2 have the value 1, 3 and 4 has 2. With that you can group by that value. Only by inserting the score you've to add an extra field. So your table looks like this:
score | user | range
--------------------------
2 | Mark | 1
4 | Alex | 2
3 | John | 2
2 | Elliot | 1
10 | Joe | 5
5 | Dude | 3
Now you can do:
select count(score),range from table group by range;
This is always faster if you've an application where selecting has prior.
By inserting do this:
$scoreRange = 2;
$range = ceil($score/$scoreRange);

mysql rank by column value grouped by column value

I am trying to figure out how to rank based on 2 different column numbers in laravel but raw mysql will do. I have a list of videos, these videos are inside of competitions and are given votes if someone likes the video. Each video will have a vote number and a competition number. I am trying to rank based on votes within competition. So for example below I have competition 8, I need the rank of all the videos in that competition based on votes. I then need the same for competition 5 etc.
|rank|votes|competition|
------------------
| 1 | 100 | 8 |
------------------
| 2 | 50 | 8 |
------------------
| 3 | 30 | 5 |
------------------
| 1 | 900 | 5 |
------------------
| 2 | 35 | 5 |
------------------
I have tried various group and selectby methods but nothing seems to work, any ideas?
In Mysql you can use user-defined variables to calculate rank,case statement checks if competition is same as the previous row then increment rank by one if different then assign 1 an order by is needed to have correct rank for the video
SELECT t.*,
#current_rank:= CASE WHEN #current_rank = competition
THEN #video_rank:=#video_rank +1
ELSE #video_rank:=1 END video_rank,
#current_rank:=competition
FROM t ,
(SELECT #video_rank:=0,#current_rank:=0) r
ORDER BY competition desc, votes desc
See Demo
If you are confused with the last extra column you can use a subselect
See Demo
You can use windowing functions:
select
competition, votes, rank() over (partition by competition order by votes desc) as rank
from
table1

Summarize db table player stats on output (multiple entries)

I'm trying to output a "scoreboard"/"standings" list of hockey players. I currently have a MySQL table like this:
statsID | matchID | playerID | goals | assists
120 | 2 | 3 | 4 | 1
121 | 2 | 4 | 2 | 2
122 | 3 | 3 | 0 | 3
So for each match the players gets added along with goals and assists. This means that there's multiple entries (several matches) for each player.
So basically, is there a good way to fetch the player data, summarize goals and assists (+ overall points) and output it in a list?
Right now, I have a basic query just outputting each row (as far as my current knowledge reaches). So how can I just output one entry of each player with his stats summarized?
Here's one approach:
SELECT t.playerID
, SUM(t.goals) AS total_goals
, SUM(t.assists) AS total_assists
FROM sometable t
GROUP BY t.playerID
ORDER BY t.playerID
Not clear what calculation you use for "overall points", if you are just adding goals and assists, then you can use this in the SELECT list:
, SUM(t.goals)+SUM(t.assists) AS overall_points
You need to use a group by clause.
SELECT playerID, SUM(goals + assist) overall points
FROM table_name
GROUP BY playerID

Categories